Home arrow Python arrow Page 4 - PyQT: Getting Started

PyQT in the Real World - Python

PyQT is used in Python for creating graphical user interfaces. This article gets you started with the basics of PyQT.

TABLE OF CONTENTS:
  1. PyQT: Getting Started
  2. PyQT: Understanding the Terminology
  3. Steps for PyQT Application Creation
  4. PyQT in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 12
March 29, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In real world applications, there would be at least two classes, one that sub-classes a container class such as QDialog and a second that would inherit the first one and set up the QApplication object. For the sake of brevity, in this discussion only one class will be used, that will be derived from the QDialog class. So let's get started.

First the imports:

import sys
from qt import *

Next the class that derives from QDialog. It is better to derive from QDialog if you require a lightweight application:

class Form1(QDialog):
:
:

Now the constructor for this class. The main parameters to look out for are the parent indicating the parent of the dialog, the name of the dialog and the flag indicating whether the dialog is modal or not.

class Form1(QDialog):
  def __init__(self,parent = None,name = None,modal =
0,fl = 0):
    QDialog.__init__(self,parent,name,modal,fl)
   :
   :

Next are the two components to be used, QLCDNumber and QSlider. Their objects are instantiated by passing their names and the equivalent of the "this" pointer of C++ - self. Then the geometry is set to set the boundaries:

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)                                           

     :
     :

Then connect the QLCDNumber and QSlider. The Signal is "valueChanged" and the slot is "display." Since the display of the QLCDNumber is the receiver and the slot, they are not separately used.

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.connect(self.slider1,SIGNAL("valueChanged
(int)"),  
                   self.lCDNumber1.display)
      :
      :

Lastly, we have the main section where QApplication will be set up:

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.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()

Thatís all. Whenever the value of the slider changes, the new value is displayed in the  QLCDNumber.

That brings us to the end of this discussion. It should be evident from the above application how easy it is to develop applications using PyQT. But this is just the beginning. In the future, I will cover other aspects, including complex widgets such as qtcanvas. Till next time.



 
 
>>> 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: