使用 PyQt 中的按钮在另一个窗口中打开文本文件 [关闭]
Posted
技术标签:
【中文标题】使用 PyQt 中的按钮在另一个窗口中打开文本文件 [关闭]【英文标题】:Opening a text file in another window using push button in PyQt [closed] 【发布时间】:2014-04-03 06:02:45 【问题描述】:这里我有两个python代码,第一个是mainwindow.py:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(559, 207)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.label = QtGui.QLabel(self.centralWidget)
self.label.setGeometry(QtCore.QRect(20, 40, 111, 17))
self.label.setObjectName(_fromUtf8("label"))
self.lineEdit = QtGui.QLineEdit(self.centralWidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 40, 411, 25))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(200, 90, 151, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 559, 23))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Enter the path :", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit.setText(QtGui.QApplication.translate("MainWindow", "Path of the text file...", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Text File", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我的另一个包含 textEdit 的窗口是 textfile.py:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_TextFile(object):
def setupUi(self, TextFile):
TextFile.setObjectName(_fromUtf8("TextFile"))
TextFile.resize(683, 531)
self.scrollArea = QtGui.QScrollArea(TextFile)
self.scrollArea.setGeometry(QtCore.QRect(10, 50, 661, 471))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 657, 467))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.textEdit = QtGui.QTextEdit(self.scrollAreaWidgetContents)
self.textEdit.setGeometry(QtCore.QRect(0, 0, 661, 461))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.label = QtGui.QLabel(TextFile)
self.label.setGeometry(QtCore.QRect(280, 20, 67, 17))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(TextFile)
QtCore.QMetaObject.connectSlotsByName(TextFile)
def retranslateUi(self, TextFile):
TextFile.setWindowTitle(QtGui.QApplication.translate("TextFile", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("TextFile", "Text File", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
TextFile = QtGui.QWidget()
ui = Ui_TextFile()
ui.setupUi(TextFile)
TextFile.show()
sys.exit(app.exec_())
我的想法是在从主窗口单击按钮时将文本文件作为新窗口打开。我的主窗口包含 linEdit 和按钮中的文件路径。单击按钮后,它应该在另一个包含 textEdit 的窗口中显示文本文件的内容。 请建议对代码进行适当的更改。
【问题讨论】:
见meta.stackexchange.com/questions/40164/… 【参考方案1】:非常简单,您只需在 mainWindow 类中创建一个连接语句和一个函数。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_TextFile(object):
def setupUi(self, TextFile,file):
self.text= open(file,"r").read()
TextFile.setObjectName(_fromUtf8("TextFile"))
TextFile.resize(683, 531)
self.scrollArea = QtGui.QScrollArea(TextFile)
self.scrollArea.setGeometry(QtCore.QRect(10, 50, 661, 471))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 657, 467))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.textEdit = QtGui.QTextEdit(self.scrollAreaWidgetContents)
self.textEdit.setGeometry(QtCore.QRect(0, 0, 661, 461))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.textEdit.setText(self.text)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.label = QtGui.QLabel(TextFile)
self.label.setGeometry(QtCore.QRect(280, 20, 67, 17))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(TextFile)
QtCore.QMetaObject.connectSlotsByName(TextFile)
def retranslateUi(self, TextFile):
TextFile.setWindowTitle(QtGui.QApplication.translate("TextFile", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("TextFile", "Text File", None, QtGui.QApplication.UnicodeUTF8))
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(559, 207)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.label = QtGui.QLabel(self.centralWidget)
self.label.setGeometry(QtCore.QRect(20, 40, 111, 17))
self.label.setObjectName(_fromUtf8("label"))
self.lineEdit = QtGui.QLineEdit(self.centralWidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 40, 411, 25))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(200, 90, 151, 27))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 559, 23))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()") , self.OpenIT)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Enter the path :", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit.setText(QtGui.QApplication.translate("MainWindow", "Path of the text file...", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Text File", None, QtGui.QApplication.UnicodeUTF8))
def OpenIT(self):
TextFile = QtGui.QDialog()
ui = Ui_TextFile()
ui.setupUi(TextFile,file=str(self.lineEdit.text()))
TextFile.exec_()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
【讨论】:
@ thecreator232 :是的,但它通过单击按钮打开简单的新窗口,但我想要的是在那个新窗口中它应该加载我们提供的文本文件的内容主窗口中的路径。 @Latik :更改完成。 @Latik:你对 python 完全陌生吗? @Latik :已纠正,再次检查。以上是关于使用 PyQt 中的按钮在另一个窗口中打开文本文件 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章