Today I learnt about the Tkinter module and what args and kwargs are. It has got me thinking about what sort of games I can make with it. Below are the notes, and the final project is at the very bottom.
Notes
notes.py
for learning about args and kwargs
# Multiple positional arguments
def add(*args):
total = 0
for n in args:
total += n
print(total)
add(3,56,78,9,78)
# Multiple keyword arguments
# Optional parameters
def calculate(n, **kwargs):
print(kwargs)
n += kwargs["add"]
n -= kwargs["sub"]
n *= kwargs["mul"]
n /= kwargs["div"]
calculate(3, add = 5)
class Drawing:
def __init__(self, **kw):
self.colour = kw.get("colour")
self.width = kw.get("width")
self.height = kw.get("height")
tkinter_notes.py
self-explanatory
from tkinter import *
print("DOCUMENTATION - https://www.tcl-lang.org/man/tcl8.6/TkCmd/contents.htm")
window = Tk()
window.title("My first GUI Program")
window.minsize(400, 300)
window.config(padx = 10, pady = 10)
# Label
my_label = Label(text="This is a label.")
my_label.pack(side="left")
# Update properties of previous component (any of these 2 methods)
my_label["text"] = "New text."
my_label.config(text = "New text.")
my_label.config(padx = 15, pady = 15)
# Entry
input = Entry(width = 20)
input.pack(side = "right")
# Button
def button_clicked():
answer = input.get()
my_label.config(text = str(answer))
button = Button(text = "A button.", command = button_clicked)
button.pack()
window.mainloop()
# pack() is straightforward to use, you can leave it at default, left, right or bottom,
# but it is very hard to precisely position something
# with place() you can add the x= and y= parameters, but it is sometimes too specific, and
# you need to work out the position in your head
# so, with gird() you can specify the row = and column =
# however, this is relative to the other entities positions on the display
# you cannot mix grid() and pack() in the same program
Tkinter_Widgets.py
notes provided by the course
from tkinter import *
#Creating a new window and configurations
window = Tk()
window.title("Widget Examples")
window.minsize(width=500, height=500)
#Labels
label = Label(text="This is old text")
label.config(text="This is new text")
label.pack()
#Buttons
def action():
print("Do something")
#calls action() when pressed
button = Button(text="Click Me", command=action)
button.pack()
#Entries
entry = Entry(width=30)
#Add some text to begin with
entry.insert(END, string="Some text to begin with.")
#Gets text in entry
print(entry.get())
entry.pack()
#Text
text = Text(height=5, width=30)
#Puts cursor in textbox.
text.focus()
#Adds some text to begin with.
text.insert(END, "Example of multi-line text entry.")
#Get's current value in textbox at line 1, character 0
print(text.get("1.0", END))
text.pack()
#Spinbox
def spinbox_used():
#gets the current value in spinbox.
print(spinbox.get())
spinbox = Spinbox(from_=0, to=10, width=5, command=spinbox_used)
spinbox.pack()
#Scale
#Called with current scale value.
def scale_used(value):
print(value)
scale = Scale(from_=0, to=100, command=scale_used)
scale.pack()
#Checkbutton
def checkbutton_used():
#Prints 1 if On button checked, otherwise 0.
print(checked_state.get())
#variable to hold on to checked state, 0 is off, 1 is on.
checked_state = IntVar()
checkbutton = Checkbutton(text="Is On?", variable=checked_state, command=checkbutton_used)
checked_state.get()
checkbutton.pack()
#Radiobutton
def radio_used():
print(radio_state.get())
#Variable to hold on to which radio button value is checked.
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Option1", value=1, variable=radio_state, command=radio_used)
radiobutton2 = Radiobutton(text="Option2", value=2, variable=radio_state, command=radio_used)
radiobutton1.pack()
radiobutton2.pack()
#Listbox
def listbox_used(event):
# Gets current selection from listbox
print(listbox.get(listbox.curselection()))
listbox = Listbox(height=4)
fruits = ["Apple", "Pear", "Orange", "Banana"]
for item in fruits:
listbox.insert(fruits.index(item), item)
listbox.bind("<<ListboxSelect>>", listbox_used)
listbox.pack()
window.mainloop()
Final Project (Miles to Kilometer Conversion)
from tkinter import *
window = Tk()
window.title("Miles to Kilometers")
window.minsize(300, 100)
window.config(padx = 5, pady = 5)
# TODO : define a function to convert miles to kilometers rounded to the nearest .1
def conversion(answer):
kilometers = float(answer) * 1.60934
kilometers = round(kilometers, 1)
return kilometers
# TODO : define a function to get the user's input and display conversion
input = Entry(width = 20)
input.grid(row=0, column=0)
label = Label(text="Miles is equals to")
label.grid(row=0, column=1)
answer_label = Label(text="0")
answer_label.grid(row=1, column=0)
final_label= Label(text="Km")
final_label.grid(row=1, column=1)
def button_clicked():
answer = input.get()
answer_label.config(text = str(conversion(answer)))
button = Button(text = "Calculate", command = button_clicked)
button.grid(row=2, column=2)
window.mainloop()
Top comments (0)