GUI Python
GUI Python
GUI Python
wegsdgsdgsdfg
We will use Python 3.6, so if you are using Python 2.x, it’s strongly
recommended to switch to Python 3.x unless you know the language
changes so you can adjust the code to run without errors.
I assume that you a little background about Python basics to help you
understand what we are doing.
We will start by creating a window then we will learn how to add widgets
such as buttons, combo boxes, etc, then we will play with their
properties, so let’s get started.
The last line which calls mainloop function, this function calls the
endless loop of the window, so the window will wait for any user
interaction till we close it.
If you forget to call the mainloop function, nothing will appear to the
user.
lbl.grid(column=0, row=0)
So the complete code will be like this:
Note that the font parameter can be passed to any widget to change its
font not labels only.
Great, but the window is so small, we can even see the title, what about
setting the window size?
window.geometry('350x200')
The above line sets the window width to 350 pixels and the height to
200 pixels.
Let’s try adding more GUI widgets like buttons and see how to handle
button click event.
Note that we place the button on the second column of the window
which is 1. If you forget and place the button on the same column which
is 0, it will show the button only, since the button will be on the top of the
label.
Now, if you tried to click on the button, nothing happens because the
click event of the button isn’t written yet.
1 def clicked():
2
3 lbl.configure(text="Button was clicked !!")
Then we will wire it with the button by specifying the function like this:
Cool!!
You can create a textbox using Tkinter Entry class like this:
txt = Entry(window,width=10)
Then you can add it to the window using grid function as usual
First, you can get entry text using get function. So we can write this
code to our clicked function like this:
1 def clicked():
2
3 res = "Welcome to " + txt.get()
4
5 lbl.configure(text= res)
If you click the button and there is a text on the entry widget, it will show
“Welcome to” concatenated with the entered text.
Awesome!!
Every time we run the code, we need to click on the entry widget to set
focus to write the text, what about setting the focus automatically?
txt.focus()
And when you run your code, you will notice that the entry widget has
the focus so you can write your text right away.
As you can see, we add the combobox items using the values tuble.
To set the selected item, you can pass the index of the desired item to
the current function.
To get the select item, you can use the get function like this:
combo.get()
1 chk_state = IntVar()
2
3 chk_state.set(0) #uncheck
4
5 chk_state.set(1) #check
These examples give the same result as the BooleanVar.
Also, you can set the command of any of these radio buttons to a
specific function, so if the user clicks on any one of them, it runs the
function code.
This is an example:
Every time you select a radio button, the value of the variable will be
changed to the value of the selected radio button.
txt.delete(1.0,END)
Great!!
Create a MessageBox
To show a message box using Tkinter, you can use messagebox library
like this:
Also, you can check what button was clicked using the result variable
If you click OK or yes or retry, it will return True value, but if you
choose no or cancel, it will return False.
The only function that returns one of three values
is askyesnocancel function, it returns True or False or None.
Also, you can specify the width of the widget using the width parameter:
1 var =IntVar()
2
3 var.set(36)
4
5 spin = Spinbox(window, from_=0, to=100, width=5, textvariable=var)
Now, if you run the program, it will show 36 as a default value for the
Spinbox.
bar['value'] = 70
You can set this value based on any process you want like downloading
a file or completing a task.
First, we will create a style and set the background color and finally set
the created style to the Progressbar.
files = filedialog.askopenfilenames()
Specify file types (filter file extensions)
You can specify the file types for a file dialog using filetypes parameter,
just specify the extensions in tubles.
dir = filedialog.askdirectory()
You can specify the initial directory for the file dialog by specifying the
initialdir like this:
You can add menu items under any menu by using add_cascade()
function like this:
menu.add_cascade(label='File', menu=new_item)
So our code will be like this:
1 from tkinter import *
2
3 from tkinter import Menu
4
5 window = Tk()
6
7 window.title("Welcome to LikeGeeks app")
8
9 menu = Menu(window)
10
11 new_item = Menu(menu)
12
13 new_item.add_command(label='New')
14
15 menu.add_cascade(label='File', menu=new_item)
16
17 window.config(menu=menu)
18
19 window.mainloop()
Using this way, you can add many menu items as you want.
Here we add another menu item called Edit with a menu separator.
You may notice a dashed line at the beginning, well, if you click that
line, it will show the menu items in a small separate window.
You can disable this feature by disabling the tearoff feature like this:
I don’t need to remind you that you can type any code that works when
the user clicks on any menu item by specifying the command property.
new_item.add_command(label='New', command=clicked)
Add a Notebook widget (tab control)
To create a tab control, there are 3 steps to do so.
You can add many tabs as you want the same way.
Just pass padx and pady to any widget and give them a value.
In this tutorial, we saw many Python GUI examples using Tkinter library
and we saw how easy it’s to develop graphical interfaces using it.
This tutorial covers main aspects of Python GUI development not all of
them. There is no tutorial or a book can cover everything.
Thank you.