PyQT: Handling Windows and Buttons - PyQT in the Real World
(Page 4 of 4 )
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:
class Form1(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
self.lCDNumber1 = QLCDNumber(self,"lCDNumber1")
self.lCDNumber1.setGeometry(QRect(110,140,301,30))
self.slider1 = QSlider(self,"slider1")
self.slider1.setGeometry(QRect(130,220,261,31))
self.slider1.setOrientation(QSlider.Horizontal)
self.Close = QPushButton(self,"&Close")
self.Close.setGeometry(QRect(250,120,90,30))
self.hex = QPushButton(self,"&hex")
self.hex.setGeometry(QRect(20,120,100,30))
self.connect(self.slider1,SIGNAL("valueChanged(int)"),
self.lCDNumber1.display)
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.
class Form1(QDialog):
def __init__(self,parent = None,name = None,modal = 0,fl = 0):
QDialog.__init__(self,parent,name,modal,fl)
self.lCDNumber1 = QLCDNumber(self,"lCDNumber1")
self.lCDNumber1.setGeometry(QRect(110,140,301,30))
self.slider1 = QSlider(self,"slider1")
self.slider1.setGeometry(QRect(130,220,261,31))
self.slider1.setOrientation(QSlider.Horizontal)
self.Close = QPushButton(self,"&Close")
self.Close.setGeometry(QRect(250,120,90,30))
self.hex = QPushButton(self,"&hex")
self.hex.setGeometry(QRect(20,120,100,30))
self.connect(self.slider1,SIGNAL("valueChanged(int)"),
self.lCDNumber1.display)
self.connect(self.hex,SIGNAL("clicked()"),
self.lCDNumber1.setHexMode)
self.connect(self.Close,SIGNAL("pressed()"),self.close)
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |