Gui in Python
Total Page:16
File Type:pdf, Size:1020Kb
GUI IN PYTHON By : Sangeeta M Chauhan , Gwalior www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Best Python GUI Frameworks which supports multiple platforms (Windows, Linux and Mac). These all GUI frameworks are easy to use and popular, some of them are open-source. • Kivy :OpenGL ES 2, supports multiple platforms namely Windows, MacOSX, Linux, Android iOS and Raspberry Pi. Open source • PyQT : Cross-platform ,Supports Unix/Linux, Windows, Mac OS X and Sharp Zaurus. It is available in both, commercial as well as GPL license. Although some features may not be available in the free version, but you can use it under the free license with open source. • WxPython open source wrapper for cross-platform GUI library WxWidgets (earlier known as WxWindows) and implemented as a Python extension module. With WxPython you as a developer can create native applications for Windows, Mac OS and Unix. If you’re just beginning to develop applications in WxPython, here is a good simple tutorial you can go through. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior • PyGUI cross-platform framework for Unix, Macintosh and Windows. lightweight,. • PySide free and cross-platform GUI toolkit Qt initiated and sponsored by Nokia, ,currently supports Linux/X11, Mac OS X, Maemo and Windows and, support for Android is in the plans for the near future. PySide provides tools to works with multimedia, XML documents, network, databases and GUI. A key feature of PySide is its API compatibility with PyQt4, so if you wish to migrate to PySide then the process will be hassle- free. • Tkinter bundled with Python, Python’s standard GUI framework. Popular for its simplicity and GUI. Open source and available under the Python License. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior To create a tkinter application: 1. Import module – tkinter 2. Create the main window (container) 3. Add any number of widgets to the main window 4. Apply the event Trigger on the widgets. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior There are two main methods to be used while creating application using tkinter •Tk (screenName=None, baseName=None, className=’Tk’, useTk=1): This creates a toplevel widget of Tk which usually is the main window of an application. Each instance has its own associated Tcl interpreter. • Mainloop() : used when you are ready for the application to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event till the window is not closed. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior To create window www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Tkinter Widgets (Controls ) Sr.No. Operator & Description 1 Button The Button widget is used to display buttons in your application. 2 Canvas The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your application. 3 Checkbutton The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time. 4 Entry The Entry widget is used to display a single-line text field for accepting values from a user. 5 Frame The Frame widget is used as a container widget to organize other widgets. 6 Label The Label widget is used to provide a single-line caption for other widgets. It can also contain images. 7 Listbox The Listbox widget is used to provide a list of options to a user. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Sr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu widget is used to provide various commands to a user. These commands are contained inside Menubutton. 10 Message The Message widget is used to display multiline text fields for accepting values from a user. 11 Radiobutton The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time. 12 Scale The Scale widget is used to provide a slider widget. 13 Scrollbar The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes. 14 Text The Text widget is used to display text in multiple lines. 15 Toplevel The Toplevel widget is used to provide a separate window container. 16 Spinbox The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values. 17 PanedWindow A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically. 18 LabelFrame A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts. 19 tkMessageBox This module is used to display message boxes in your applications. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Before learining how to handle widgets lets understand 3 important manager classes • pack() method:It organizes the widgets in blocks before placing in the parent widget. • grid() method:It organizes the widgets in grid (table-like structure) before placing in the parent widget. • place() method:It organizes the widgets by placing them on specific positions directed by the programmer. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Button:To add a button in your application, this widget is used. The general syntax is: w=Button(master, option=value) • Master: parameter used to represent the parent window. • Options: There are number of options which are used to change the format of the Buttons. Number of options can be passed as parameters separated by commas. Some of them are listed below. – activebackground: to set the background color when button is under the cursor. – activeforeground: to set the foreground color when button is under the cursor. – bg: to set he normal background color. – command: to call a function. – font: to set the font on the button label. – image: to set the image on the button. – width: to set the width of the button. – height: to set the height of the button. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Lets create a Button in window Button is created www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior PACK() Geometry Manager The Pack geometry manager packs widgets in rows or columns. We can use options like fill, expand, and side to control this geometry manager. expand − When set to true, widget expands to fill any space not otherwise used in widget's parent. fill − Determines whether widget fills any extra space allocated to it by the packer, or keeps its own minimal dimensions: NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically). side − Determines which side of the parent widget packs against: TOP (default), BOTTOM, LEFT, or RIGHT. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior PACK with Button This examples creates 4 Buttons (without functionality) with text in different colours www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Pack() with expand option Button 1 is expandedd with frame www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior To change position of Button use grid function Positioning of Buttons in grid Message box will be displayed on click of “OK Button www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior List of some basic controls (Widgets) of tkinter w = Entry (parent, options) – For Text Field single line w = Text (parent, option, ... )- For Text Field Multi line w = Label (parent, option, ... )- For Label w = Button (parent, option, ... )- For Button w = Message (parent , option, ... ) - For message Box w = Checkbutton (parent, option, ... )- For CheckBox w = Radiobutton (parent, option, ... )-For RadioButton w = Listbox (parent , option, ... )- For List Box www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Some Common Options used with tkinter Widgets OPTION VALUE EFFECT foreground (fg) Colour Changes the foreground colour. (Colour may be specified using pre-defined keywords or using RGB values) background (bg) Colour Changes the background colour. (Colour may be specified using pre-defined keywords or using RGB values) bd (border width) integer Specifies the width of the widget border command Callbacktype Specifies which method is to be executed when the widget is selected. font Font type Specifies the font used by the widget. Often represented in a tuple (family, size, weight) padx, pady Integer Specifies the width between the current widget and the neighbor widgets. relief Relief type: Specifies the relief of the widget. •GROOVE •SUNKEN •RIDGE •FLAT text string Specifies the text appearing on the widget at run time www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Lets implement some of the widgets through examples A function is defined to display a message when Button is clicked Defining action : Button will call Function ‘WelcomeMsg()” and pass E1 object(TextField) www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior OUTPUT OF PREVIOUS PROGRAM When User Clicks on Button it will display a Message Box www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Lets create a List Box containing Python Topics output www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior This example shows how to create Option Button and Radio Button These functions are showing which Checkbox or Radio Button is Selected ………..Continued to next page www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior ………..Continued from previous page Variables are created to store values for checkboxes and radiobuttons Functions are called when widgets(controls) are selected Label is created to display message using functions sel(),selchk1(),selchk2()….. www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Output Message displayed when radio button or checkbox is selected www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior In the same way you can use other control To View More Programs using tkinter follow the link: 1.