Python PyQt5在消息窗口中显示文件内容
我有一个info.txt文件,我想在单击info时显示内容Python PyQt5在消息窗口中显示文件内容,python,pyqt,Python,Pyqt,我有一个info.txt文件,我想在单击info时显示内容 import sys from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QMessageBox from PyQt5.QtGui import QIcon file=open("info.txt","r") data=file.read() class Example(QMainWindow): def showdia
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QMessageBox
from PyQt5.QtGui import QIcon
file=open("info.txt","r")
data=file.read()
class Example(QMainWindow):
def showdialog(self):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(data)
msg.setWindowTitle("Info")
msg.show()
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
textEdit = QTextEdit()
self.setCentralWidget(textEdit)
sourceAction = QAction(QIcon('info.png'), 'More informations', self)
sourceAction.setShortcut('Ctrl+I')
sourceAction.setStatusTip('More info')
self.statusBar()
sourceAction.triggered.connect(self.showdialog)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&About')
fileMenu.addAction(sourceAction)
sourceAction.triggered.connect(self.showdialog)
toolbar = self.addToolBar('Exit')
toolbar.addAction(sourceAction)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('Summoner info')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
file.close()
消息框不会显示info.txt文件的内容,几乎会立即消失。您正在将
showdialog
定义为类示例的一种方法。
这个
但是,在本地和全局名称空间中查找showdialog
,其中未定义该名称空间。
相反,将信号连接到以下方法:
sourceAction.triggered.connect(self.showdialog)
另外,您还必须至少将self
作为方法中的参数。我还添加了一个msg.show()
call-否则您的消息框将不可见
def showdialog(self):
msg = QMessageBox(self) # use self as parent here
# and/or keep a reference to the messagebox
self.messageBox = msg
# etc...
# actually show the message box:
msg.show()
编辑:添加了self
作为messagebox的父级-因此它不会被回收。您正在将showdialog
定义为类的方法示例
。
这个
但是,在本地和全局名称空间中查找showdialog
,其中未定义该名称空间。
相反,将信号连接到以下方法:
sourceAction.triggered.connect(self.showdialog)
另外,您还必须至少将self
作为方法中的参数。我还添加了一个msg.show()
call-否则您的消息框将不可见
def showdialog(self):
msg = QMessageBox(self) # use self as parent here
# and/or keep a reference to the messagebox
self.messageBox = msg
# etc...
# actually show the message box:
msg.show()
编辑:添加了self
作为messagebox的父项-因此它不会被回收。您需要在使用它之前定义它:现在解析器在定义它之前访问符号showdialog
。编辑:另外,如果您想将其作为函数调用,它应该是showdialog()
,但它仍然表示它未定义……您是否可以按当前的状态对您的问题进行修改,这就是它不起作用的原因。在使用它之前,您需要先定义它:现在解析器在定义它之前先访问符号showdialog
。编辑:另外,如果您想将其作为函数调用,它应该是showdialog()
,但它仍然表示它未定义…您是否可以编辑您的问题并对其进行更改,因为它目前无法正常工作谢谢,但是消息框没有显示info.txt的内容,并且几乎立即消失……我猜这是因为你的消息框没有父对象,并且你没有在你的类中保留对msg
的引用。因此,该对象被回收,messagebox被销毁。使用父项创建messagebox应该可以解决这个问题。请检查我的更新答案msg=QMessageBox(self)
而不是简单地QMessageBox
将self
设置为父项。我创建了一个新的操作来关闭程序,但什么也没有发生。fermer.triggered.connect(QCoreApplication.instance().quit)谢谢,但是消息框没有显示info.txt的内容,几乎立即消失……我猜这是因为你的消息框没有父对象,你也没有在类中保留对msg
的引用。因此,该对象被回收,messagebox被销毁。使用父项创建messagebox应该可以解决这个问题。请检查我的更新答案msg=QMessageBox(self)
而不是简单地QMessageBox
将self
设置为父项。我创建了一个新的操作来关闭程序,但什么也没有发生。fermer.triggered.connect(QCoreApplication.instance().quit)