Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5在消息窗口中显示文件内容_Python_Pyqt - Fatal编程技术网

Python PyQt5在消息窗口中显示文件内容

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

我有一个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 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)