Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

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

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.

This is the interface I have created using PyQt4 Designer. Here is the Image link :

a

and I want to launch this windows when I click on currency button. Here is the Image Link:

b

Here is my code for main.py

from PyQt4 import QtGui
from main_screen import mainscreen

def main():
    import sys
    qApp = QtGui.QApplication(sys.argv)
    aw = mainscreen()
    aw.show()
    sys.exit(qApp.exec_())

if __name__ == '__main__':
    main()

and code for mainscreen.py

from PyQt4 import QtCore, QtGui
from window_main import Ui_MainWindow

class mainscreen(QtGui.QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
                super(mainscreen,self).__init__(parent)
                self.ui = Ui_MainWindow()
                self.ui.setupUi(self)

How can I open new window after I click on currency button (object name for currency button is "currency_bt") and do I have to write the code for currency in same window or I have to write in new window. How do I do it.

I am new to Python Gui programming.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...