Chap-4] GUI Programming and Database Connectivity Using Python
Chap-4] GUI Programming and Database Connectivity Using Python
Elements of a GUI?
Benefits of GUI?
Microsoft Windows
Apple System 7 and macOS
Chrome OS
1. Apple macOS
2. Microsoft Windows
3. GNOME
4. KDE
Example
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc.
that are used to build the python GUI applications.
SN Widget Description
4 Entry The entry widget is used to display the single-line text field
to the user. It is commonly used to accept user values.
13 Scrollbar It provides the scrollbar to the user so that the user can
scroll the window up and down.
The Label is used to specify the container box where we can place the
text or images.
This widget is used to provide the message to the user about other
widgets used in the python application.
There are the various options which can be specified to configure the text
or the part of the text shown in the Label.
Syntax
w = Label (master, options)
SN Option Description
1 anchor It specifies the exact position of the text within the size
provided to the widget. The default value is CENTER, which
is used to center the text within the specified space.
5 cursor The mouse pointer will be changed to the type of the cursor
specified, i.e., arrow, dot, etc.
6 font The font type of the text written inside the widget.
14 text This is set to the string variable which may contain one or
more line of text.
15 textvariable The text written inside the widget is set to the control
variable StringVar so that it can be accessed and changed
accordingly.
16 underline We can display a line under the specified letter of the text.
Set this option to the number of the letter under which the
line will be displayed.
18 wraplength Instead of having only one line as the label text, we can
break it to the number of lines where each line has the
number of characters specified to this option.
Output:
Text
The Text widget is used to show the text data on the Python
application.
However, Tkinter provides us the Entry widget which is used to
implement the single line text box.
The Text widget is used to display the multi-line formatted text with
various styles and attributes.
The Text widget is mostly used to provide the text editor to the user.
We can also use the windows and images with the Text as it can also
be used to display the formatted text.
Syntax
w = Text (top, options)
A list of possible options that can be used with the Text widget is given
below.
SN Option Description
10 highlighcolor The color of the focus highlight when the widget has
the focus.
Output
Button
Syntax
W = Button (parent, options)
SN Option Description
10 Highlightcolor The color of the highlight when the button has the
focus.
Message box
Syntax
messagebox.function_name(title, message [, options])
Parameters
o function_name: It represents an appropriate message box function.
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
message box.
o options: There are various options which can be used to configure the
message dialog box.
1. showinfo()
Example
Output
2. showwarning()
This method is used to display the warning to the user. Consider the
following example.
Example
Output
3. showerror()
This method is used to display the error message to the user. Consider
the following example.
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.
Example
Output
5. askokcancel()
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.
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
Radiobutton
Syntax
w = Radiobutton (top, options)
SN Method Description
2 flash() It is used to flash the radiobutton between its active and normal
colors few times.
Example:
#radioButton Demo
from tkinter import *
def selection():
selection = "You selected the option " + str(radio.get())
label.config(text = selection)
top = Tk()
top.geometry("300x150")
top.title('MainPage')
radio = IntVar()
lbl = Label(text = "Favourite programming language:")
lbl.pack()
R1 = Radiobutton(top, text="C", variable=radio, value=1,
command=selection)
R1.pack(anchor = W)
R2 = Radiobutton(top, text="C++", variable=radio, value=2,
command=selection)
R2.pack( anchor = W )
R3 = Radiobutton(top, text="Java", variable=radio, value=3,
command=selection)
R3.pack( anchor = W)
label = Label(top)
label.pack()
top.mainloop()
Checkbutton
Syntax
Example:
#checkButton Demo
from tkinter import *
top = Tk()
top.geometry("200x200")
checkvar1 = IntVar()
checkvar2 = IntVar()
checkvar3 = IntVar()
chkbtn1.pack()
chkbtn2.pack()
chkbtn3.pack()
top.mainloop()
Syntax
w = Entry (parent, options)
SN Option Description
Example
#Input Demo
from tkinter import *
top = Tk()
top.geometry("400x250")
top.title('MainPage')
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
Navigate your command line to the location of PIP, and type the
following:
demo_mysql_test.py:
import mysql.connector
Example
import mysql.connector
#Create the connection object
myconn = mysql.connector.connect(host = "localhost",
user = "root",
passwd = "google")
Mr. D. S. Kiwde Page 25
MMN Python
Output:
Example
import mysql.connector
Output: