这里我有两个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和pushbutton中的文件路径。单击按钮后,它应该在另一个包含textEdit的窗口中显示文本文件的内容。 请建议代码中的正确更改。
答案 0 :(得分:0)
非常简单,只需在mainWindow类中创建一个connect语句和一个函数。
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_())