0% found this document useful (0 votes)
2 views5 pages

Python Micro Unit 4

The document discusses GUI programming in Python, focusing on the Tkinter module and its various widgets such as buttons, labels, and text fields. It explains the benefits of using graphical user interfaces over command-line interfaces, highlighting their user-friendliness and ease of use. Additionally, it provides syntax and options for creating and configuring different GUI elements.

Uploaded by

dipakkhillare040
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Python Micro Unit 4

The document discusses GUI programming in Python, focusing on the Tkinter module and its various widgets such as buttons, labels, and text fields. It explains the benefits of using graphical user interfaces over command-line interfaces, highlighting their user-friendliness and ease of use. Additionally, it provides syntax and options for creating and configuring different GUI elements.

Uploaded by

dipakkhillare040
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MMN Python MMN Python MMN Python

Chap – 4] GUI Programming and Database Connectivity Using Elements of a GUI?  Linux variants like Ubuntu using a GUI interface.
Python
4.1] Graphical User Interfaces To make a GUI as user-friendly as possible, there are different Examples of a GUI interface
 A GUI (graphical user interface) is a system of interactive visual elements and objects that the user uses to interact with the software.
components for computer software. 1. Apple macOS
 A GUI displays objects that convey information, and represent actions Below is a list of each of these with a brief description.
that can be taken by the user. 2. Microsoft Windows
 The objects change color, size, or visibility when the user interacts  Button - A graphical representation of a button that performs an
with them. action in a program when pressed 3. GNOME
 The GUI was first developed at Xerox PARC by Alan Kay, Douglas  Dialog box - A type of window that displays additional information,
Engelbart, and a group of other researchers in 1981. and asks a user for input. 4. KDE
 Later, Apple introduced the Lisa computer with a GUI on January  Icon - Small graphical representation of a program, features, or file.
Menu - List of commands or choices offered to the user through the 5. Any Microsoft program, including Word, Excel, and Outlook.
19, 1983. 
 GUI is often pronounced by saying each letter (G-U-I or gee-you-eye). menu bar.
6. Internet browsers, such as Internet Explorer, Chrome,
 It sometimes is also pronounced as "gooey."  Menu bar - Thin, horizontal bar containing the labels of menus.
and Firefox.
 A GUI includes GUI objects, like icons, cursors, and buttons.  Ribbon - Replacement for the file menu and toolbar that groups
 These graphical elements are sometimes enhanced with sounds, or programs activities together.
4.2] Using the tkinter Module
visual effects like transparency and drop shadows. Using these  Tab - Clickable area at the top of a window that shows another page
 Python provides the standard library Tkinter for creating the graphical
objects, a user can use the computer without having to know or area.
user interface for desktop based applications.
commands.  Toolbar - Row of buttons, often near the top of an application window
that controls software functions.
An empty Tkinter top-level window can be created by using the following
Below is a picture of the Windows 7 desktop and an example of a  Window - Rectangular section of the computer's display that shows
steps.
GUI operating system. In this example, you could use a mouse to the program currently being used.
move a pointer and click a program icon to start a program.
Benefits of GUI? 1. import the Tkinter module.
2. Create the main application window.
 A GUI is considered to be more user-friendly than a text- 3. Add the widgets like labels, buttons, frames, etc. to the window.
based command-line interface, such as MS-DOS, or the shell of Unix-
4. Call the main event loop so that the actions can take place on the
like operating systems. user's computer screen.
 Unlike a command-line operating system or CUI, like Unix or MS-DOS,
Example
GUI operating systems are easier to learn and use because commands
do not need to be memorized.
 Additionally, users do not need to know any programming languages.
Because of their ease of use and more modern appearance, GUI
operating systems have come to dominate today's market.

Examples of a GUI operating system

 Microsoft Windows
 Apple System 7 and macOS
 Chrome OS

Mr. D. S. Kiwde Page 1 Mr. D. S. Kiwde Page 2 Mr. D. S. Kiwde Page 3

MMN Python MMN Python MMN Python

Tkinter widgets 14 Text It is different from Entry because it provides a multi-line 3 bitmap It is used to set the bitmap to the graphical object specified
text field to the user so that the user can write the text so that, the label can represent the graphics instead of text.
There are various widgets like button, canvas, checkbutton, entry, etc.
and edit the text inside it.
that are used to build the python GUI applications. 4 bd It represents the width of the border. The default is 2 pixels.
14 Toplevel It is used to create a separate window container.
5 cursor The mouse pointer will be changed to the type of the cursor
SN Widget Description
15 Spinbox It is an entry widget used to select from options of values. specified, i.e., arrow, dot, etc.
1 Button The Button is used to add various kinds of buttons to the 16 PanedWindow It is like a container widget that contains horizontal or 6 font The font type of the text written inside the widget.
python application. vertical panes.
7 fg The foreground color of the text written inside the widget.
2 Canvas The canvas widget is used to draw the canvas on the 17 LabelFrame A LabelFrame is a container widget that acts as the
window. container 8 height The height of the widget.
3 Checkbutton The Checkbutton is used to display the CheckButton on the 18 MessageBox This module is used to display the message-box in the 9 image The image that is to be shown as the label.
window. desktop based applications.
10 justify It is used to represent the orientation of the text if the text
4 Entry The entry widget is used to display the single-line text field contains multiple lines. It can be set to LEFT for left
to the user. It is commonly used to accept user values. 4.3] Creating Label, Text, Button, info Dialog Boxes, Radiobutton, justification, RIGHT for right justification, and CENTER for
Checkbutton center justification.
5 Frame It can be defined as a container to which, another widget Label
can be added and organized. 11 padx The horizontal padding of the text. The default value is 1.
 The Label is used to specify the container box where we can place the
6 Label A label is a text used to display some message or 12 pady The vertical padding of the text. The default value is 1.
text or images.
information about the other widgets.
 This widget is used to provide the message to the user about other
widgets used in the python application. 13 relief The type of the border. The default value is FLAT.
7 ListBox The ListBox widget is used to display a list of options to
the user. 14 text This is set to the string variable which may contain one or
There are the various options which can be specified to configure the text
more line of text.
8 Menubutton The Menubutton is used to display the menu items to the or the part of the text shown in the Label.
user. 15 textvariable The text written inside the widget is set to the control
The syntax to use the Label is given below.
variable StringVar so that it can be accessed and changed
9 Menu It is used to add menu items to the user. accordingly.
Syntax
10 Message The Message widget is used to display the message-box to w = Label (master, options) 16 underline We can display a line under the specified letter of the text.
the user. Set this option to the number of the letter under which the
A list of possible options is given below. line will be displayed.
11 Radiobutton The Radiobutton is different from a checkbutton. Here, the
user is provided with various options and the user can SN Option Description 17 width The width of the widget. It is specified as the number of
select only one option among them. characters.
1 anchor It specifies the exact position of the text within the size
12 Scale It is used to provide the slider to the user. provided to the widget. The default value is CENTER, which 18 wraplength Instead of having only one line as the label text, we can
is used to center the text within the specified space. break it to the number of lines where each line has the
13 Scrollbar It provides the scrollbar to the user so that the user can number of characters specified to this option.
scroll the window up and down. 2 bg The background color displayed behind the widget.

Mr. D. S. Kiwde Page 4 Mr. D. S. Kiwde Page 5 Mr. D. S. Kiwde Page 6


MMN Python MMN Python MMN Python

A list of possible options that can be used with the Text widget is given
17 pady The vertical padding of the widget.
below.
18 relief The type of the border. The default is SUNKEN.
SN Option Description
19 selectbackground The background color of the selected text.
1 bg The background color of the widget. 20 selectborderwidth The width of the border around the selected text.
2 bd It represents the border width of the widget. 21 spacing1 It specifies the amount of vertical space given above
each line of the text. The default is 0.
3 cursor The mouse pointer is changed to the specified cursor
type, i.e. arrow, dot, etc. 22 spacing2 This option specifies how much extra vertical space
to add between displayed lines of text when a logical
4 exportselection The selected text is exported to the selection in the
line wraps. The default is 0.
window manager. We can set this to 0 if we don't
Output: want the text to be exported. 23 spacing3 It specifies the amount of vertical space to insert
below each line of the text.
5 font The font type of the text.
24 state It the state is set to DISABLED, the widget becomes
6 fg The text color of the widget.
unresponsive to the mouse and keyboard
7 height The vertical dimension of the widget in lines. unresponsive.

8 highlightbackground The highlightcolor when the widget doesn't has the 25 tabs This option controls how the tab character is used to
focus. position the text.

9 highlightthickness The thickness of the focus highlight. The default 26 width It represents the width of the widget in characters.
value is 1.
27 wrap This option is used to wrap the wider lines into
10 highlighcolor The color of the focus highlight when the widget has multiple lines. Set this option to the WORD to wrap
Text the lines after the word that fit into the available
the focus.
space. The default value is CHAR which breaks the
 The Text widget is used to show the text data on the Python line which gets too wider at any character.
11 insertbackground It represents the color of the insertion cursor.
application.
 However, Tkinter provides us the Entry widget which is used to 12 insertborderwidth It represents the width of the border around the 28 xscrollcommand To make the Text widget horizontally scrollable, we
implement the single line text box. cursor. The default is 0. can set this option to the set() method of Scrollbar
 The Text widget is used to display the multi-line formatted text with widget.
various styles and attributes. 13 insertofftime The time amount in Milliseconds during which the
 The Text widget is mostly used to provide the text editor to the user. insertion cursor is off in the blink cycle. 29 yscrollcommand To make the Text widget vertically scrollable, we
 We can also use the windows and images with the Text as it can also can set this option to the set() method of Scrollbar
be used to display the formatted text. 14 insertontime The time amount in Milliseconds during which the widget.
insertion cursor is on in the blink cycle.
The syntax to use the Text widget is given below.
15 insertwidth It represents the width of the insertion cursor.
Syntax
16 padx The horizontal padding of the widget.
w = Text (top, options)

Mr. D. S. Kiwde Page 7 Mr. D. S. Kiwde Page 8 Mr. D. S. Kiwde Page 9

MMN Python MMN Python MMN Python

Syntax 15 Relief It represents the type of the border. It can be


W = Button (parent, options) SUNKEN, RAISED, GROOVE, and RIDGE.

A list of possible options is given below. 17 State This option is set to DISABLED to make the button
unresponsive. The ACTIVE represents the active state
of the button.
SN Option Description
18 Underline Set this option to make the button text underlined.
1 activebackground It represents the background of the button when the
mouse hover the button. 19 Width The width of the button. It exists as a number of
letters for textual buttons or pixels for image buttons.
2 activeforeground It represents the font color of the button when the
mouse hover the button. 20 Wraplength If the value is set to a positive number, the text lines
will be wrapped to fit within this length.
3 Bd It represents the border width in pixels.

Output 4 Bg It represents the background color of the button.

5 Command It is set to the function call which is scheduled when


the function is called.

6 Fg Foreground color of the button.

7 Font The font of the button text.

8 Height The height of the button. The height is represented in


the number of text lines for the textual lines or the
number of pixels for the images.

10 Highlightcolor The color of the highlight when the button has the
focus.

Button 11 Image It is set to the image displayed on the button.

 The button widget is used to add various types of buttons to the 12 justify It illustrates the way by which the multiple text lines
python application. are represented. It is set to LEFT for left justification,
 Python allows us to configure the look of the button according to our RIGHT for the right justification, and CENTER for the
requirements. center.

We can also associate a method or function with a button which is called 13 Padx Additional padding to the button in the horizontal
when the button is pressed. direction.

14 pady Additional padding to the button in the vertical


The syntax to use the button widget is given below.
direction.

Mr. D. S. Kiwde Page 10 Mr. D. S. Kiwde Page 11 Mr. D. S. Kiwde Page 12


MMN Python MMN Python MMN Python

Message box Output 3. showerror()

 The messagebox module is used to display the message boxes in the This method is used to display the error message to the user. Consider
python applications. the following example.
 There are the various functions which are used to display the relevant
messages depending upon the application requirements. Example

The syntax to use the messagebox is given below.

Syntax
messagebox.function_name(title, message [, options])

2. showwarning()
Parameters
o function_name: It represents an appropriate message box function. This method is used to display the warning to the user. Consider the
following example.
o title: It is a string which is shown as a title of a message box.
o message: It is the string to be displayed as a message on the Example
message box. Output
o options: There are various options which can be used to configure the
message dialog box.

1. showinfo()

 The showinfo() messagebox is used where we need to show some


relevant information to the user.

Example
Output

4. askquestion()

This method is used to ask some question to the user which can be
answered in yes or no. Consider the following example.

Mr. D. S. Kiwde Page 13 Mr. D. S. Kiwde Page 14 Mr. D. S. Kiwde Page 15

MMN Python MMN Python MMN Python

Example Output 7. askretrycancel()

This method is used to ask the user about doing a particular task again
or not. Consider the following example.

Example

Output

6. askyesno()

This method is used to ask the user about some action to which, the
user can answer in yes or no. Consider the following example. Output

Example

5. askokcancel()

This method is used to confirm the user's action regarding some


application activity. Consider the following example.
Radiobutton

Output  The Radiobutton widget is used to implement one-of-many selection in


the python application.
 It shows multiple choices to the user out of which, the user can select
only one out of them.
 We can display the multiple line text or images on the radiobuttons.

The syntax to use the Radiobutton is given below.

Syntax
w = Radiobutton (top, options)

The radiobutton widget provides the following methods.

Mr. D. S. Kiwde Page 16 Mr. D. S. Kiwde Page 17 Mr. D. S. Kiwde Page 18


MMN Python MMN Python MMN Python

chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue


SN Method Description = 1, offvalue = 0, height = 2, width = 10)

1 deselect() It is used to turn of the radiobutton. chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2,
onvalue = 1, offvalue = 0, height = 2, width = 10)
2 flash() It is used to flash the radiobutton between its active and normal
colors few times. chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3,
onvalue = 1, offvalue = 0, height = 2, width = 10)
3 invoke() It is used to call any procedure associated when the state of a
Radiobutton is changed.
[Link]()
4 select() It is used to select the radiobutton.
[Link]()
Checkbutton
Example: [Link]()
#radioButton Demo  The Checkbutton is used to track the user's choices provided to the
from tkinter import * application. [Link]()
 In other words, we can say that Checkbutton is used to implement the
def selection(): on/off selections.
selection = "You selected the option " + str([Link]())  The Checkbutton can contain the text or images.
[Link](text = selection)  The Checkbutton is mostly used to provide many choices to the user
among which, the user needs to choose the one.
top = Tk()  It generally implements many of many selections.
[Link]("300x150")
[Link]('MainPage') The syntax to use the checkbutton is given below.
radio = IntVar()
lbl = Label(text = "Favourite programming language:") Syntax
[Link]()
R1 = Radiobutton(top, text="C", variable=radio, value=1, w = checkbutton (master, options)
command=selection) 4.4] Getting Input
[Link](anchor = W) Example:  The Entry widget is used to provide the single line text-box to the user
R2 = Radiobutton(top, text="C++", variable=radio, value=2, #checkButton Demo to accept a value from the user.
command=selection) from tkinter import *  We can use the Entry widget to accept the text strings from the user.
[Link]( anchor = W )  It can only be used for one line of text from the user.
R3 = Radiobutton(top, text="Java", variable=radio, value=3, top = Tk()  For multiple lines of text, we must use the text widget.
command=selection)
[Link]( anchor = W) [Link]("200x200") The syntax to use the Entry widget is given below.
label = Label(top)
[Link]() checkvar1 = IntVar() Syntax
[Link]() w = Entry (parent, options)
checkvar2 = IntVar()

checkvar3 = IntVar()

Mr. D. S. Kiwde Page 19 Mr. D. S. Kiwde Page 20 Mr. D. S. Kiwde Page 21

MMN Python MMN Python MMN Python

A list of possible options is given below. sbmitbtn = Button(top, text = "Submit",activebackground = "pink",
then the cursor doesn't blink: it is on all the time.
activeforeground = "blue").place(x = 30, y = 170)
13 insertontime Specifies a non-negative integer value indicating the e1 = Entry(top).place(x = 80, y = 50)
SN Option Description e2 = Entry(top).place(x = 80, y = 90)
number of milliseconds the insertion cursor should
remain "on" in each blink cycle. e3 = Entry(top).place(x = 95, y = 130)
1 bg The background color of the widget. [Link]()
14 insertwidth It represents the value indicating the total width of the Output:
2 bd The border width of the widget in pixels.
insertion cursor. The value may have any of the forms
3 cursor The mouse pointer will be changed to the cursor type acceptable to Tk_GetPixels.
set to the arrow, dot, etc.
15 justify It specifies how the text is organized if the text
4 exportselection The text written inside the entry box will be contains multiple lines.
automatically copied to the clipboard by default. We
can set the exportselection to 0 to not copy this. 16 relief It specifies the type of the border. Its default value is
FLAT.
5 fg It represents the color of the text.
17 selectbackground The background color of the selected text.
6 font It represents the font type of the text.
18 selectborderwidth The width of the border to display around the selected
7 highlightbackground It represents the color to display in the traversal task.
highlight region when the widget does not have the
input focus. 19 selectforeground The font color of the selected task.
4.5] Importing MySQL for Python

8 highlightcolor It represents the color to use for the traversal highlight 20 show It is used to show the entry text of some other type
 Python needs a MySQL driver to access the MySQL database.
rectangle that is drawn around the widget when it has instead of the string. For example, the password is
 In this tutorial we will use the driver "MySQL Connector".
the input focus. typed using stars (*).
 We recommend that you use PIP to install "MySQL Connector".
21 textvariable It is set to the instance of the StringVar to retrieve the  PIP is most likely already installed in your Python environment.
9 highlightthickness It represents a non-negative value indicating the width
of the highlight rectangle to draw around the outside of text from the entry.
Navigate your command line to the location of PIP, and type the
the widget when it has the input focus. following:
22 width The width of the displayed text or image.
10 insertbackground It represents the color to use as background in the
23 xscrollcommand The entry widget can be linked to the horizontal
area covered by the insertion cursor. This color will
scrollbar if we want the user to enter more text then
normally override either the normal background for the
the actual width of the widget.
widget.

11 insertborderwidth It represents a non-negative value indicating the width Example


of the 3-D border to draw around the insertion cursor. #Input Demo  Now you have downloaded and installed a MySQL driver.
The value may have any of the forms acceptable to from tkinter import *
Tk_GetPixels. top = Tk()
[Link]("400x250") Test MySQL Connector
12 insertofftime It represents a non-negative integer value indicating [Link]('MainPage')
 To test if the installation was successful, or if you already have
the number of milliseconds the insertion cursor should name = Label(top, text = "Name").place(x = 30,y = 50)
remain "off" in each blink cycle. If this option is zero, email = Label(top, text = "Email").place(x = 30, y = 90) "MySQL Connector" installed, create a Python page with the following
password = Label(top, text = "Password").place(x = 30, y = 130) content:

Mr. D. S. Kiwde Page 22 Mr. D. S. Kiwde Page 23 Mr. D. S. Kiwde Page 24


MMN Python MMN Python

demo_mysql_test.py: #printing the connection object


import [Link] print(myconn)

 If the above code was executed with no errors, "MySQL Connector" is Output:
installed and ready to be used.
4.6] Connecting with a database <[Link] object at 0x7fb142edd780>
 Here, we must notice that we can specify the database name in the
In this section, we will discuss the steps to connect the python connect () method if we want to connect to a specific database.
application to the database.

There are the following steps to connect a python application to our Example
database. import [Link]

1. Import [Link] module


#Create the connection object
2. Create the connection object.
myconn = [Link](host = "localhost",
3. Create the cursor object user = "root",passwd = "google", database = "mydb")
4. Execute the query
#printing the connection object
 To create a connection between the MySQL database and the python
print(myconn)
application, the connect () method of [Link] module is used.
 Pass the database details like HostName, username, and the database
password in the method call. The method returns the connection Output:
object.
<[Link] object at 0x7ff64aa3d7b8>
The syntax to use the connect () is given below.

Connection-Object = [Link] (host = <hostname>,


user = <username> ,
passwd = <password> )

Consider the following example.

Example
import [Link]
#Create the connection object
myconn = [Link](host = "localhost",
user = "root",
passwd = "google")
Mr. D. S. Kiwde Page 25 Mr. D. S. Kiwde Page 26

You might also like