GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton...

26
GUI IN PYTHON By : Sangeeta M Chauhan , Gwalior www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Transcript of GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton...

Page 1: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

GUI IN PYTHON

By : Sangeeta M Chauhan , Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Page 2: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

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

Page 3: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

• PyGUI cross-platform framework for Unix, Macintosh andWindows. lightweight,.

• PySide free and cross-platform GUI toolkit Qt initiated andsponsored by Nokia, ,currently supports Linux/X11, Mac OS X,Maemo and Windows and, support for Android is in the plansfor the near future. PySide provides tools to works withmultimedia, XML documents, network, databases and GUI. Akey feature of PySide is its API compatibility with PyQt4, so ifyou wish to migrate to PySide then the process will be hassle-free.

• Tkinter bundled with Python, Python’s standard GUIframework. Popular for its simplicity and GUI. Open sourceand available under the Python License.

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Page 4: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

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

Page 5: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

There are two main methods to be used while creatingapplication 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 Tclinterpreter.

• 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

Page 6: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

To create window

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Page 7: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

Tkinter Widgets (Controls )Sr.No. Operator & Description

1 ButtonThe Button widget is used to display buttons in your application.

2 CanvasThe Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles,in your application.

3 CheckbuttonThe Checkbutton widget is used to display a number of options as checkboxes. The usercan select multiple options at a time.

4 EntryThe Entry widget is used to display a single-line text field for accepting values from auser.

5 FrameThe Frame widget is used as a container widget to organize other widgets.

6 LabelThe Label widget is used to provide a single-line caption for other widgets. It can alsocontain images.

7 ListboxThe Listbox widget is used to provide a list of options to a user.

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Page 8: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

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 commandsare contained inside Menubutton.

10 Message The Message widget is used to display multiline text fields for accepting valuesfrom a user.

11 Radiobutton The Radiobutton widget is used to display a number of options as radiobuttons. 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, suchas 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 canbe used to select from a fixed number of values.

17 PanedWindow A PanedWindow is a container widget that may contain any number ofpanes, arranged horizontally or vertically.

18 LabelFrame A labelframe is a simple container widget. Its primary purpose is to act as aspacer 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

Page 9: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

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

Page 10: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

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

Page 11: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

Lets create a Button in window

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Button is created

Page 12: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

PACK() Geometry Manager

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

The Pack geometry manager packs widgets in rows or columns.We can use options like fill, expand, and side to control thisgeometry 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.

Page 13: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

PACK with Button

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

This examples creates 4 Buttons

(without functionality) with text in

different colours

Page 14: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

Pack() with expand option

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Button 1 is expandeddwith frame

Page 15: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

To change position of Button use grid function

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Positioning of Buttons in grid

Message box will be displayed on click of “OK

Button

Page 16: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

List of some basic controls (Widgets) of tkinter

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

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

Page 17: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

OPTION VALUE EFFECTforeground (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 bordercommand 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:•GROOVE•SUNKEN•RIDGE•FLAT

Specifies the relief of the widget.

text string Specifies the text appearing on the widget at run time

Some Common Options used with tkinter Widgets

Page 18: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

Lets implement some of the widgets through examples

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

A function is defined to display a message

when Button is clicked

Defining action : Button will call Function

‘WelcomeMsg()” and pass E1 object(TextField)

Page 19: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

OUTPUT OF PREVIOUS PROGRAM

When User Clicks on Button it will display a Message

Box

Page 20: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

Lets create a List Box containing Python Topics

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

output

Page 21: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

This example shows how to create Option Button and Radio Button

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

These functions are showing

which Checkbox or Radio Button

is Selected

………..Continued to next page

Page 22: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

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

Page 23: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

Output

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Message displayed when radio button or checkbox is selected

Page 24: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

To View More Programs using tkinter follow the link:1. https://pythonclassroomdiary.wordpress.com/gui-programstkinter/2. https://pythonclassroomdiary.wordpress.com/python-gui-projects/

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

In the same way you can use other control

Page 25: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

References

• https://www.tutorialspoint.com/

• https://effbot.org/tkinterbook/

• https://www.python-course.eu/

Page 26: GUI IN PYTHON - pythonclassroomdiary.files.wordpress.comSr.No. Operator & Description 8 Menubutton The Menubutton widget is used to display menus in your application. 9 Menu The Menu

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior