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:
There's no need to change source for turtle creation and starting points. We will just change custom functions that deals with random movements:
def main():
for x in range(600):
t1.fd(random.randrange(10))
#t1.shapesize(15, 15, 15)
t2.fd(random.randrange(10))
#t2.shapesize(15, 15, 15)
if (t1.position()[1]) >= 300:
print("Turtle 1 WINS")
break
elif (t2.position()[1]) >= 300:
print("Turtle 2 WINS")
break
To speed things up, we are using value 10 as argument for randrange() function.
Then we are introducing if / elif check to detect when any of turtles t1 and t2 reaches position 300 verticaly. How is that done ?
When we say t1.position()[1] >= 300 that means we are checking if t1 is reaching 300 value or more on y (vertical) axis. If that is correct, t1 is winner and there is no need to check more, that's why we need to break.
If t2 reaches 300 or more vertically, same story, t2 is winner and there's break again.
Results will be printed in Python Shell.
This is end of this tutorial, and corresponding YouTube playlist.