How do I open sub window after I click on button on main screen in PyQt4

后端 未结 3 1562
你的背包
你的背包 2020-12-10 09:23

HI I am trying to make a simple converter. I have used PyQt4 designed to make the Gui I want to know how launch a new window after I click on the individual button.

相关标签:
3条回答
  • 2020-12-10 09:26

    Each GUI form that you create in Qt Designer needs to be converted into a python module using pyuic. So, to start with, you need to do the same for currency.ui that you did for window_main.

    Now you can import your currency window class into mainscreen.py, and connect a button to handler so you can display it.

    The code would look something like this:

    from PyQt4 import QtCore, QtGui
    from window_main import Ui_MainWindow
    from currency import Ui_CurrencyWindow
    
    class CurrencyWindow(QtGui.QMainWindow, Ui_CurrencyWindow):
        def __init__(self, parent=None):
            super(CurrencyWindow, self).__init__(parent)
            self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
            self.setupUi(self)
    
    class MainScreen(QtGui.QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super(MainScreen, self).__init__(parent)
            self.setupUi(self)
            self.currencyButton.clicked.connect(self.handleCurrencyButton)
    
        def handleCurrencyButton(self):
            window = CurrencyWindow(self)
            window.show()
    

    After looking at this example code, it will probably occur to you that you are going to end up importing a lot of modules, and have a lot of boiler-plate code to write for each one of them (which is not much fun).

    So I would advise you to consider changing your GUI design, so that you have one main window containing a tabwidget, and then have a separate tab for each of your converters. This will not only make your application much easier to write, but it should also make it a lot nicer to use.

    0 讨论(0)
  • 2020-12-10 09:27

    Make two programs: main_win.py and second_win.py, then in main_win.py put this lines:

    from os import system as sh               //In the begin
    
    def openewin(self):                       //In the class main_win
        sh("python second_win.py") 
    

    Ready, just connect the push button to function openewin!

    0 讨论(0)
  • 2020-12-10 09:40

    I'm making my bachelor thesis in PyQt4. First I also wanted to use the designer (generating code is nice), but afterall I was not using it during my work. Maybe it's a matter of taste. But for your question (I did this without the QtDesigner):

    Let's say we have a main window class:

    import sys
    from PyQt4 import QtCore, QtGui
    
    class mainscreen(QtGui.QMainWindow):
            def __init__(self, parent=None):
                super(mainscreen,self).__init__(parent)
    
                self.button = QtGui.QPushButton("push")
                self.button.clicked.connect(self.pushed)
    
            @pyqtSlot()
            def pushed(self):
                # in this section here you can create the new window and show it
    
    
     qApp = QtGui.QApplication(sys.argv)
        aw = mainscreen()
        aw.show()
        sys.exit(qApp.exec_())
    

    There are some good tutorials (http://zetcode.com/gui/pyqt4/ helped me getting started).

    0 讨论(0)
提交回复
热议问题