How to open a sub window after clicking a button on the main screen in PyQt4
I am trying to make a simple converter. I have used PyQt4 designed to create Gui I want to know how to start a new window after clicking on a separate button.
This is an interface that I created using PyQt4 Designer. Here is the link to the image:
and I want to launch these windows when I click the currency button. Here is the link to the image:
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 the code 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 do I open a new window after clicking the currency button (the object name for the currency button is "currency_bt") and I need to write the code for the currency in the same window, or I need to write in a new window. How to do it.
I am new to Python Gui programming.
source to share
Every GUI form you create in Qt Designer needs to be converted to a python module using pyuic. So, first you need to do the same for currency.ui
what you did for window_main
.
Now you can import the currency window class into mainscreen.py
and wire the button to a handler so you can display it.
The code looks 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()
Looking at this example code, it will probably occur to you that you will end up importing a lot of modules and have a lot of codeplates to write down for each one (which is not much fun).
Therefore, I would advise you to consider changing the GUI design so that you have one main window containing a tabwidget and then a separate tab for each of your converters. This will not only simplify your application, but also make it much more enjoyable to use.
source to share
I am doing my bachelor's degree in PyQt4. At first I also wanted to use a constructor (generate nice code), but after that I didn't use it during my work. Maybe it's a matter of taste. But for your question (I did it without 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 get started.)
source to share