HomePython Page 4 - PyQT: Handling Windows and Buttons
PyQT in the Real World - Python
In this article, you will continue to learn about the process of building a GUI in PyQT. Specifically, you will learn about QDialog and QPushButton, which handle windows and buttons, the building blocks of most GUIs.
I mentioned at the beginning that I will be enhancing the application developed in the previous article. The enhancements include a button to change the mode of the display to hexadecimal format, and another button to close the application.
So here it is. Only the differences from the previous application are is bolded:
if __name__ == "__main__": a = QApplication(sys.argv) QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()")) w = Form1() a.setMainWidget(w) a.exec_loop()
The close button is defined by the Close QPushButton object. The ampersand before the "C" is to make C the hotkey. The setGeometry can be ignored for the time being. I will be discussing it in the future. Next is the button for changing the mode of the QLCDNumber. The hex QPushButton object is the button for changing the mode.
Next comes the connections to make the buttons into what is required.
if __name__ == "__main__": a = QApplication(sys.argv) QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()")) w = Form1() a.setMainWidget(w) a.exec_loop()
The QLCDNumber has a slot to change the mode to hexadecimal mode. It is setHexMode. Connecting the clicked signal of the hex button to the setHexMode Slot of QLCDNumber does the trick. Next is the closing of the dialog. I had already mentioned that QDialog doesn't have any slots of its own. But it inherits one from QObject. The slot is close. It is to this slot that the clicked signal of the Close button is connected.
That completes the enhanced application. And this brings us to the end of this discussion. If the application just enhanced is compared with an application having the same functionalities written in any other language, it can be observed that PyQT does much with a small amount of code. That is the beauty of this toolkit. In an upcoming article I will discuss the text boxes along with radio buttons and checkboxes.