First, let's make shure that our tkinter module is working ok with simple for loop that will spawn 5 instances of blank Tk window.
We will import all options from tkinter module because we are using only one module.
If using multiple modules, if we have some big scripts, we can use only specific methods from those modules. With that approach we are evading possible name clashes:
from tkinter import *
for x in range(5):
Tk()
After using 3 lines from above, if you see all 5 windows on screen, all is ok with import.
Then we can proceed with import of messagebox, alias mb:
import tkinter.messagebox as mb
We need to initialize a blank window with this line:
top_win = Tk()
Then, geometry of a window is set o be 500 x 500:
top_win.geometry("500x500")
Function do_something() is needed to show msg box. Inside msg box, we will have mb activated targeting showinfo() method to show "lorem ipsum stuff" as Title in a window:
def do_something():
mb.showinfo("Title", "lorem ipsum stuff")
It's not enough just to have our custom function, we need to activate it on button. A name for button will be but_1.
That button is located in top_win, and text inside it will be "Yeah". To connect button with function we are using command = do_something, without parenthesis:
but_1 = Button(top_win, text = "Yeah", command = do_something)
Of course, button but_1 must be positioned using coordinates x and y, using method place():
but_1.place(x = 2, y = 2)
Mainloop is needed at the bottom of script:
top_win.mainloop()
This is full script:
from tkinter import *
import tkinter.messagebox as mb
top_win = Tk()
top_win.geometry("500x500")
def do_something():
mb.showinfo("Title", "lorem ipsum stuff")
but_1 = Button(top_win, text = "Yeah", command = do_something)
but_1.place(x = 2, y = 2)
top_win.mainloop()
In this tutorial we will talk about Tkinter Text Widget. Make sure that you can run this source, and than we will explain.
Please note, we are reading a textual file LICENCE.txt, which is located in Python install folder - this script must be in a folder where that txt file resides, too. You can set a path to some other txt file.
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("800x800")
file = open("LICENSE.txt", "r")
t = Text(top_win, padx = 20, pady = 20, width = 50, height = 20,
cursor = "dot", relief = RIDGE, bd = 10, bg = "lightyellow",
fg = "red", selectbackground = "yellow", selectforeground = "black",
font = "Verdana")
for x in file.read():
t.insert(END, x)
file.close()
t.pack(side = TOP, anchor = NW)
top_win.mainloop()
This is line that will open LICENCE.txt for reading:
file = open("LICENSE.txt", "r")
And this is source for Text Widget. We have padding set, width and height set, type of cursor, type of relief, border width, background color, text color, font type is Verdana, and background and foreground color set when we select part of text:
We can also have menu which is found in most of the editors. Make sure that this source is working on your machine.
Once you run it, what is clicked will be printed in Python Shell. This is just a simple presentation how to connect buttons to vertical menus. In real life scenario our functions will be probably more advanced than printing stuff on screen.
Radiobuttons are easy, just as Checkbuttons. First, make sure that this simple source is working, and then we will explain:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("800x800")
lab_1 = Label(top_win, text = "Select only one: ").pack(anchor = W)
real_stuff = IntVar()
rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff, value = 1)
rad_1.pack(anchor = W)
rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2)
rad_2.pack(anchor = W)
rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3)
rad_3.pack(anchor = W)
top_win.mainloop()
To explain what needs to be done we will have Tkinter label:
lab_1 = Label(top_win, text = "Select only one: ").pack(anchor = W)
Place that will hold choice will be real_stuff:
real_stuff = IntVar()
Then we will have 3 radiobuttons, where all of them will point to real_stuff. Radio buttons will have dedicated value: 1, 2 and 3. This is important:
rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff, value = 1)
rad_1.pack(anchor = W)
rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2)
rad_2.pack(anchor = W)
rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3)
rad_3.pack(anchor = W)
What about function that will process values ? Again, make sure that code works, and then we will explain:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("800x800")
def selected():
selected = real_stuff.get()
output.config(text = selected)
if selected == 1:
print("Selected: Python at position", selected)
if selected == 2:
print("Selected: C++ at position", selected)
if selected == 3:
print("Selected: Perl at position", selected)
lab_1 = Label(top_win, text = "Select only one: ").pack(anchor = W)
real_stuff = IntVar()
rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff,
value = 1, command = selected)
rad_1.pack(anchor = W)
rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2,
command = selected)
rad_2.pack(anchor = W)
rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3,
command = selected)
rad_3.pack(anchor = W)
output = Label(top_win, text = "You Selected: ")
output.pack(anchor = W)
top_win.mainloop()
Function selected() will get values from real_stuff using get() method.
Then it will set what is captured as config text for output. Output is external Label where results will be printed, in Tkinter Window.
Also, based on choice, values will be printed in Python Shell using if checks:
def selected():
selected = real_stuff.get()
output.config(text = selected)
if selected == 1:
print("Selected: Python at position", selected)
if selected == 2:
print("Selected: C++ at position", selected)
if selected == 3:
print("Selected: Perl at position", selected)
All radio buttons now have connection to function using "command = ":
rad_1 = Radiobutton(top_win, text = "Python", variable = real_stuff,
value = 1, command = selected)
rad_1.pack(anchor = W)
rad_2 = Radiobutton(top_win, text = "C++", variable = real_stuff, value = 2,
command = selected)
rad_2.pack(anchor = W)
rad_3 = Radiobutton(top_win, text = "Perl", variable = real_stuff, value = 3,
command = selected)
rad_3.pack(anchor = W)
And than we will need 2 variables that will be used to hold values:
turbo_10 = IntVar()
turbo_20 = IntVar()
Now we need 2 Checkbuttons. Please note variable = that is connected to corresponding variables. Checkbuttons, as other Tkinter widgets must be positioned. In this case with grid option:
Make sure that you can run this code, and then we will explain it. You will see results in Python Shell:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("800x800")
def activate_boost():
status_10 = turbo_10.get()
status_20 = turbo_20.get()
if status_10 == 1:
print("Boost +10 Activated")
if status_20 == 1:
print("Boost +20 Activated")
if status_10 == 1 and status_20 == 1:
print("!" * 50)
print("ALERT - DANGER")
print("Lower Down Boost Levels - Just One Checkbutton")
if status_10 == 0 and status_20 == 0:
print("Normal Operation")
gen_lab = Label(top_win, text = "Engine Boost")
gen_lab.grid(row = 0, column = 0)
turbo_10 = IntVar()
turbo_20 = IntVar()
ch_10 = Checkbutton(top_win, text = "Boost +10", variable = turbo_10)
ch_10.grid(row = 1, column = 0)
ch_20 = Checkbutton(top_win, text = "Boost +20", variable = turbo_20)
ch_20.grid(row = 2, column = 0)
but_1 = Button(top_win, text = "Activate Boost", command = activate_boost)
but_1.grid(row = 3, column = 0)
top_win.mainloop()
This is our custom function that will process what is checked:
def activate_boost():
status_10 = turbo_10.get()
status_20 = turbo_20.get()
if status_10 == 1:
print("Boost +10 Activated")
if status_20 == 1:
print("Boost +20 Activated")
if status_10 == 1 and status_20 == 1:
print("!" * 50)
print("ALERT - DANGER")
print("Lower Down Boost Levels - Just One Checkbutton")
if status_10 == 0 and status_20 == 0:
print("Normal Operation")
With status_10 and status_20 we will get choice. Then we are checking if only one status is set to 1, and printing appropriate text.
If both of statuses are 1, than we print a message: "Lower Down Boost Levels - Just One Checkbutton".
If both statuses are 0, which means they are not operational, than some imaginary machine is working in a normal mode, no any boost, and we are printing "Normal operation".
It's easy to create listboxes using Tkinter. In most cases having label for that list will be helpful, so let's create it and we will use grid option for positioning:
Ok, now we will set Listbox that will deal with, for example, domain names. That listbox will be in top_win, as with all Tkinter widgets in most simple cases:
list_domains = Listbox(top_win)
Once we have it, we can populate a list with values using insert() method, stating item position in a list, and corresponding values:
Now we will create function that will delete selected domain from a list. We are using curselection() method for item selection, and delete() method to remove selected item:
Ok, to create Frame we must (as with other Tkinter widgets) place it into some window. In this case primary window will be top_win, with geometry 800 x 800.
To create a border around Frame we will say highlightthickness = 1, we can also set background color:
We will create function that will get data for a name and last name using get() method. After that function will just create simple Python Shell report:
In this tutorial we will combine knowledge from previous scripts.
We have only one Label with corresponding Entry field. There is also submit button, but it's not yet connected to real function that will do processing.
Place where we will see results will be inside dedicated Canvas. This is important to note.
Once we have that arrangement, we can continue with dedicated function that will do something.
Ok, now it is the time to create function that will get values from entry field with get() method. Once we have data, we can do some simple calculations.
But we also need to have again Canvas to show results there using lab_2 label that deals with text values. This is whole source of function:
In this tutorial we will learn how to have Entry fields in Tkitner Canvas. Users need explanations what to type in entry fields so we must create Labels.
First, we will create Canvas with lightgreen background. Position for Canvas is in top_win of size 800 x 800:
Think about Tkinter Canvas as a place where we can have other shapes. That means that we need coordinates and dimensions.
In this case we will have red shape, which will be in primary, top_win, with 800 x 800 dimensions. Width will be 200 and height will be 200. As with other Tkinter elements, it's not enough to have something, we must position it.
In this example, we will use Grids to position elements. Think about Grids as Excel cells. Those cells are determined by intersection of rows and columns:
Another option is to experiment with pixels. Placing Canvases py pixels demands in most cases a little bit more time, but at the end of the day we can be happy with precision:
Ok, now it's time to pay attention. We will spawn a new window in our Tkinter app by clicking on button.
To do that we will need dedicated function. Just as with a primary window, for spawned window we need to set geometry, which in most cases will be smaller than for a primary app. In this case, 300 x 300.
In that new function we will have independent button that will call some normal function, like to ping Google, or something simple like that.
As with any button, we need to pack it. At the end of net_window() function we will have mainloop:
If you don't understand a previous tutorial, give yourself some time. There is no purpose in code memorization, just try to understand an approach. Perhaps code before looks a little bit complicated, bit in fact it is not.
A little bit bigger example
In this example we are improving previous code in such ways that we can have one more button that will spawn new window, and in that window we will have some normal function which will run Notepad. Function to spawn new Notepad window will be this one: