什么';我的Python 3/PyQt5代码有什么问题?
我有这段代码,想在按下按钮后进行一些计算,并在QTextEdit小部件(ted)中显示结果。一切正常,但如果我运行程序并按下按钮(按钮1),它就会崩溃 知道我的代码有什么问题吗 谢谢你的回答 代码如下:什么';我的Python 3/PyQt5代码有什么问题?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我有这段代码,想在按下按钮后进行一些计算,并在QTextEdit小部件(ted)中显示结果。一切正常,但如果我运行程序并按下按钮(按钮1),它就会崩溃 知道我的代码有什么问题吗 谢谢你的回答 代码如下: import sys import numpy as np import matplotlib.pyplot as plt from scipy import stats from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
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)
ted = QTextEdit(self)
ted.move(50, 250)
ted.resize(500, 250)
ted.setReadOnly(True)
scrb = ted.verticalScrollBar()
scrb.setValue(scrb.maximum())
self.show()
@pyqtSlot()
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
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_())
无法从单击1时的
访问ted
变量,必须使用self实例才能使用它,将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_())
包括确切的错误消息。没有确切的错误消息,除了:Python已停止工作。然后程序窗口自动关闭…:-(非常好用。非常感谢!!!你帮了我这么多…:-))