我有这个代码,想按下按钮并在QTextEdit小部件(ted)中显示结果后进行一些计算。一切都运作良好,但如果我运行程序并按下按钮(按钮1)它会崩溃。
知道我的代码有什么问题吗?
非常感谢您的回答!
代码在这里:
{{1}}
答案 0 :(得分:0)
无法从ted
访问on_click1
变量,您必须使用自我实例来使用它,将ted
替换为self.ted
。
完整代码:
import sys
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog, QTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'STATISTICIAN'
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(100, 100, 600, 600)
button1 = QPushButton('1D statistics', self)
button1.setToolTip('Start calculations of 1D statistics')
button1.move(50, 25)
button1.resize(500,70)
button1.clicked.connect(self.on_click1)
button2 = QPushButton('2D statistics', self)
button2.setToolTip('Start calculations of 2D statistics')
button2.move(50, 105)
button2.resize(500,70)
button2.clicked.connect(self.on_click2)
self.ted = QTextEdit(self)
self.ted.move(50, 250)
self.ted.resize(500, 250)
self.ted.setReadOnly(True)
scrb = self.ted.verticalScrollBar()
scrb.setValue(scrb.maximum())
self.show()
def on_click1(self):
# Reading of data from file will be here
# Some calculations will be here
text = 'Results are:' # There will be finally more text
self.ted.insertPlainText(text) # I'm trying to show results in QTextEdit (ted)
def on_click2(self):
print('2D statistics')
print()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())