[go: up one dir, main page]

0% found this document useful (0 votes)
39 views6 pages

Development of Graphic Interfaces With Tkinter

The document explains how to create graphical interfaces with Tkinter in Python. It describes how to add labels, buttons, text boxes and organize them in a main window using the grid method. The basic parameters for each element are explained and a final example of a simple form with first name, last name and email is included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Development of Graphic Interfaces With Tkinter

The document explains how to create graphical interfaces with Tkinter in Python. It describes how to add labels, buttons, text boxes and organize them in a main window using the grid method. The basic parameters for each element are explained and a final example of a simple form with first name, last name and email is included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Development of graphical interfaces with Tkinter (Labels, Buttons, etc.

Tkinter is a Python module that provides objects for the development of User Interfaces.
The Tkinter module
Add the module to our python script in the first line of code:

from Tkinter import *


from Tkinter import *
The Main Window
To create the main window we use the syntax “window = Tk()” , window will inherit all
Tkinter methods. To initialize the window we use “window.mainloop()” and with this our
window will be launched.

from Tkinter import *


window = Tk()
window.mainloop()

The line window = Tk() starts the script and the line window.mainloop() ends it, therefore all
the code to add the other functionalities must come after the first and before the last, that is,
before window = Tk () and after window.mainloop().
If we want to give the window a title, we use the title() method, that is: window.title('title of
my window' ).

from Tkinter import *


window = Tk()
window.title('title of my window')
window.mainloop()
Label : Using Labels

Labels in Tkinter are made with the Label(window, text) object and to position it in the window
with the grid(row, column) method.
The first parameter of the Label object is the window instance and the text variable is assigned
a string value between 2 quotes.
To position it we will use the grid(row, columns) method:

from Tkinter import *


window = Tk()
window.title('Labels')
label1=Label(window,text="Enter to Tkinter")
label1.grid(row=1,column=1)
window.mainloop()

Working with the Grid method

row =1 , row =1 , columns=2 row =1 , columns=3 row =1 , row =1 , column


columns=1 columns=4 =5
row =2 , row =2 , row =2 , row =2 , row =2 ,
column=1 column=2 column=3 column=4 column=5
row =3 , row =3 , row =3 , row =3 , row =3 ,
column=1 column=2 column=3 column=4 column=5
row =4 , row =4 , row =4 , row =4 , row =4 ,
column=1 column=2 column=3 column=4 column=5

The values for the grid method can be the same as those in the table depending on the position
where you want to put the element.
Button : Using Buttons

To use a button we use the same parameters as to add a label: the instance of the window and
the text value of the button, apart from that the button has 3 extra parameters, 2 to modify the
appearance of the button: bg and relief, and one parameter to modify the behavior of the button
when pressed: command.

from Tkinter import *


window = Tk()
window.title('using Buttons')
button1 = Button(window,text="Button 1")
button1.grid(row=1,column=1)
window.mainloop()

We can change the button background, even add another button.


To change the button color we will use the parameter bg='color' where color can be replaced by
an English color: red, blue, green, lime... etc.
And to add another button we use another variable to differentiate it from the first. To set a new
position we will use row=1, column=2.

from Tkinter import *


window = Tk()
window.title('using Buttons')
button1 = Button(window,text="Button 1",bg='red')
button1.grid(row=1,column=1)
button2 = Button(window,text="Button 2",bg='blue')
button2.grid(row=1,column=2)
window.mainloop()
There are several ways to view a button, for a raised button the relief parameter is used and it
can take already defined constant values which are: RAISED, SUNKEN, RIDGE, SOLID,
FLAT, GROOVE.

from Tkinter import *


window = Tk()
window.title('using Buttons')
button1 = Button(window,text="NORMAL Button")
button1.grid(row=1,column=1)
button2 = Button(window,text="FLAT Button",relief=FLAT)
button2.grid(row=1,column=2)
button3 = Button(window,text="SUNKEN Button",relief=SUNKEN)
button3.grid(row=1,column=3)
button4 = Button(window,text="RIDGE Button",relief=RIDGE)
button4.grid(row=1,column=4)
button5 = Button(window,text="Button SOLID",relief=SOLID)
button5.grid(row=1,column=5)
window.mainloop()

Entry: Using Text Boxes


Text boxes are usually the most basic form of data entry when working with graphical
interfaces, with Tkinter the Entry method is used, although with a difference in terms of the
parameters used by the Entry method.

The parameters are Entry(window, textvariable), the window variable is the instance variable
of the Tk() object and the value that textvariable carries is a variable that is an instance of
StringVar() , which will have the value of what is written in the Text's box :

from Tkinter import * window=Tk()


window.title('Using Entry')
string_variable = StringVar()
box = Entry(window,textvariable=string_variable)
box.grid(row=1,column=1)
window.mainloop()
Although normally a text box is accompanied by a label to know what the data that must be
inserted in the box is about.

from Tkinter import *


window=Tk()
window.title('Using Entry')
label1=Label(window,text="User: ")
label1.grid(row=1,column=1)
string_variable = StringVar()
box = Entry(window, textvariable=string_variable)
box.grid(row=1,column=2)
window.mainloop()

Finishing
With these basic graphic elements we can make many applications and to start the development
we will end up with a form that asks for name, surname and email, although it will not do
anything special yet.

from Tkinter import *


root = Tk()

root.title('form 1')
# row 1 : the name
label_name = Label(root,text="Name:")
label_name.grid(row=1,column=1)
str_name = StringVar()
entry_name = Entry(root,textvariable=str_name)
entry_name.grid(row=1,column=2)
# row 2 : the last name
last_label= Label(root,text="Last Name: ")
last_label.grid(row=2,column=1)
last_str = StringVar()
last_entry = Entry(root,textvariable=last_str)
last_entry.grid(row=2,column=2)
# row 3 : the email
mail_label = Label(root,text="Email : ")
mail_label.grid(row=3,column=1)
mail_str = StringVar()
mail_entry = Entry(root,textvariable=mail_str)
mail_entry.grid(row=3,column=2)
# row 4 : end
finish = Button(root,text="finish",relief=FLAT)
finish.grid(row=4,column=2)
root.mainloop()

You might also like