Flag This Hub

Simple Calendar Using Python and Qt Designer

By


Why Python and Qt ?

As we all know , Python is an easy-to-use and evolving language that is lovely to use.Python saves you a lot of development time.

Qt - a cross-platform framework originally developed by Trolltech ( now acquired by Nokia ) - needs no introduction.It is the parent of KDE and SymbianOS.It is not a mere GUI toolkit , but an entire stack of technologies.

In this tutorial , i just wanted to show you how to make a working Calendar application in 2 minutes.

QtDesigner

QtDesigner showing the calender design
See all 2 photos
QtDesigner showing the calender design

Designing the UI

Make sure you install PyQt in your computer.The installation will provide a designing tool - QtDesigner .

  1. Open QtDesigner
  2. Choose New -> Widget . Click "Create".Now a form window will open
  3. In the side pane , many GUI objects ( widgets ,views etc) are shown . Drag the Calender widget onto the form.
  4. That's it . Resize it and change its properties (in right side pnae ) as per your requirement and save the form and close Designer.

Now the coding part....

Converting Design into Python Code

This is not difficult as you would imagine.The PyQt devlopers have provided us a very convenient tool - pyuic4 - that would convert our Designs into Python code!

  1. Open Command prompt and go to the directory containing the design (.ui file)
  2. Type this : pyuic4 yourdesign.ui > target.py In my case , pyuic4 calendar.ui > calendar.py .
  3. Open the python file ( calendar.py ) , you can see all your design specs have been converted into code.

Now, you need a python file to show your application.Let's do that....

Show the Application

This is the application launch file (calendar_show.py).

Explanation:

Setting up the UI just takes about 5 lines of code.

Headers:

Ui_Form is the class in the calendar.py file ( created by pyuic4 )

QtGui and QtCore - the core modules of Qt , are imported.

Class:

Your class should inherit the main container. It can be a MainWindow or Dialog or Widget . In our case , a Widget . Thus QtGui.QWidget is inherited.

Then initialize the parent class and then setup the UI using setupUi() method.

Code:

import sys
from PyQt4 import QtGui,QtCore
from calender import Ui_Form

class startCalender(QtGui.QWidget):
  def __init__(self,parent=None):
    QtGui.QWidget.__init__(self,parent)
    self.ui=Ui_Form()
    self.ui.setupUi(self)

if __name__=="__main__":
  app=QtGui.QApplication(sys.argv)
  cal=startCalender()
  cal.show()
  sys.exit(app.exec_())


Launching:

You have to create a QApplication object to run any Qt application.Then create a Calender object and finally show the app using show() method.

Launch the app by typing "python calender_show.py" in command prompt.Your application is now up and running.

This app does absolutely nothing , but i have just shown how it can be done.If any problem/doubts , ask me . Next tutorial will be a simple web browser in PyQt.

Our App
Our App

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working