Domanda

Here I have the two python code, the first is 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_())

And my another window which contains textEdit is 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_())

Here my idea is to open the text file as a new window when pushbutton is clicked from mainwindow. my main window contains file path in linEdit and pushbutton. After clicking the push button it should show the contents of text file in another window which contains textEdit. please suggest the proper changes in code.

È stato utile?

Soluzione

Its very simple you just have create a connect statement and a function in your mainWindow class.

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_())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top