|
发表于 2024-4-4 22:24:27
|
显示全部楼层
非常抱歉,我在之前的代码示例中遗漏了初始化`code`属性。请尝试以下修正后的代码:
- import random
- from datetime import date
- from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("Code Randomizer")
- self.code = [
- "print('Hello, World!')",
- "x = 5",
- "y = 10",
- "print(x + y)",
- # 其他行的代码...
- ]
- self.target_date = date(2024, 1, 1)
- self.check_date()
- def check_date(self):
- current_date = date.today()
- if current_date >= self.target_date:
- self.randomize_code()
- self.show_warning_dialog()
- def randomize_code(self):
- lines_to_delete = random.randint(1, 100)
- line_indices = random.sample(range(len(self.code)), lines_to_delete)
- for index in sorted(line_indices, reverse=True):
- self.code.pop(index)
- for line in self.code:
- exec(line)
- def show_warning_dialog(self):
- msg_box = QMessageBox()
- msg_box.setWindowTitle("警告")
- msg_box.setText("程序已过期,请联系开发者更新版本!")
- msg_box.setIcon(QMessageBox.Warning)
- msg_box.setStandardButtons(QMessageBox.Ok)
- msg_box.exec_()
- if __name__ == "__main__":
- app = QApplication()
- window = MainWindow()
- app.exec()
复制代码
在这段修正后的代码中,我们在`MainWindow`类的构造函数中添加了`self.code`属性,并初始化为包含一些示例代码的列表。这样,在调用`randomize_code()`方法时就不会出现`AttributeError: 'WindowA' object has no attribute 'code'`错误了。
请记得根据你的实际需求修改`code`属性中的代码内容。如果还有其他问题,请随时向我提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|