Python
  Home arrow Python arrow Page 4 - PyQT: Handling Windows and Buttons
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

PyQT: Handling Windows and Buttons
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-04-05

    Table of Contents:
  • PyQT: Handling Windows and Buttons
  • Slots
  • QPushButton
  • PyQT in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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.

       · In the second part of the PyQT series I have discussed the two of the most common...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway