Home arrow Python arrow 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.

TABLE OF CONTENTS:
  1. PyQT: Handling Windows and Buttons
  2. Slots
  3. QPushButton
  4. PyQT in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 8
April 05, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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. 



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: