Tkinter Flashcards
Import Tkinter
Create a window
Put text at the top of the box
Start the box
create root window
from tkinter import *
root = Tk()
root.title(“Welcome to GameShark”)
root.geometry(‘350x200’)
root.mainloop()
What is a label? Add one.
Don’t forget to add the function/class? that tells it where to go in the window!
create root window
A label is just a line of text.
from tkinter import *
root = Tk()
root.title(“Welcome to GameShark”)
root.geometry(‘350x200’)
lbl = Label(root, text = “GameShark has a bid DICK”)
lbl.grid()
root.mainloop()
Create a button, that when clicked, displays your text.
Don’t forget, you have to add code to tell the button and your label where to go now!
from tkinter import *
root = Tk()
root.title(“Welcome to GameShark”)
root.geometry(‘350x200’)
lbl = Label(root, text = “Gameshark is the BEST”)
lbl.grid()
txt = Entry(root, width=10)
txt.grid(column=1, row=0)
def clicked():
res = “you wrote “ + txt.get()
lbl.configure(text = res)
btn = Button(root, text = “Enter”, fg = “red”, command=clicked)
btn.grid(column=2, row=0)
root.mainloop()
Add a menu bar to your gui
add menu bar in root window
from tkinter import *
root = Tk()
root.title(“Welcome to Gameshark”)
root.geometry(‘350x200’)
#new item in menu bar labelled ‘New’
#add more items to the menu
menu = Menu(root)
item = Menu(menu)
item.add_command(label=’New’)
menu.add_cascade(label=’File’, menu=item
root.config(menu=menu)
lbl = Label(root, text = ‘Is GameShark the best?”)
lbl.grid()
#below that is where it’s placed
txt = Entry(root, width=10)
txt.grid(column=1, row=0)
def clicked():
res = "You wrote " + txt.get() lbl.configure(text = res)
btn = Button(root, text = “Click me”, fg = “red”, command=clicked)
btn.grid(column=2, row=0)
root.mainloop()
Create a background
if we don’t use pack, the next label will just go underneath the image
(toolkit interface)
from tkinter import *
root = Tk()
root.title(“GameShark”)
root.geometry(“500x500”)
lbl = Lable(root, text = “Hello, and welcome”, font=(‘Georgia’, 24,)
lbl.pack()
image_path = PhotoImage(file=r”C:\image\myimage.png”)
bg_image = Label(root, image=image_path)
# 0.5 means make image take up 50% of screen, 1 means the whole screen.
bg_image.place(relheight=0.5,relwidth=1)
Add Sounds
from tkinter import *
from playsound import playsound
def play():
playsound(r”C:\Users\joshc\Downloads\sword-sound-2-36274.mp3”)
root = Tk()
root.title(“GameShark”)
root.geometry(“500x500”)
menubar = Menu(root)
file_menu = Menu(menubar, tearoff=0)
file_menu.add_command(label=”Share with Brian”)
menubar.add_cascade(label=”Sharing”, menu=file_menu)
btn = Button(root, text=”Cut Noise”, command=play)
btn.grid(column=0,row=0)
root.config(menu=menubar)
root.mainloop()
What does pack mean?
Use it
Pack this in the first available spot. This is unsophisticated.
This will keep your text on the same spot no matter how you resize, which is nice.
lbl.pack()
How do you use grid
lbl1.grid(column=0,row=0)
lbl2.grid(row=1,column=5)
This will just put this in column one because it’s all relative to the first grid.
You could actually create this space one way doing something like this:
lbl3 = Label(root, text=””)
lbl3.grid(row=1, column=1)
Create a disabled button
Resize button
btn = Button(root, text=”click me”, state=DISABLED)
btn = Button(root, text=”click me”, padx=50, pady=50)
Create an entry widget and give it a width and height and give it a border
Put some default text inside your text box.
What function do you use to retrieve your entered text?
e = Entry(root, width=50,height=50,borderwidth=10)
e.insert(0, “Enter your name: “)
e.get()
Create a calculator
We’ll have a row of three numbers, so we’ll want to have the entry span those columns
Next time you do this, be sure to take the bits that you don’t know from the cal.py file and add them to flash cards so you’ll know the pieces of how to do this in the future.
https://www.youtube.com/watch?v=YXPyB4XeYLA
When placing an “Entry” widget, what option would you add if you wanted the box to span accross 3 buttons below it?
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
Create a global variable if function is called.
def button_divide():
global math
math = “division”
Delete what’s in an entry field
e = Entry()
e.delete(0, END)
What option allows you to input an argument from a button onto a function?
command=lambda: button_click(1)
To resize a button, where do you add your padx and pady
Directly to the button, not the grid, the grid will just create space.
What is an Icon file called?
ICO which is just a PNG
Add an icon and an exit button
from tkinter import *
root = Tk()
root.title(“Gameshark”)
root.iconbitmap(r”C:\Users\joshc\Downloads\1805914c0f1e9c653168d23775b91be4.ico”)
button_quit = Button(root, text=”Exit Program”, command=root.quit)
button_quit.pack()
root.mainloop()
Add an image, because the built in image thingy only does two types of images
pip install Pillow
pip freeze (look for Pillow)
from PIL import ImageTk,Image
my_img = ImageTk.PhotoImage(Image.open(“aspen.png”)
my_label = Label(image=my_img)
my_label.pack
you also don’t need to specify directories if in same folder as program.
Say you have created and image but you want it removed when you click a button, what would you do to the label to make it work?
lbl.grid_forget()
If you wanted to create a label that said “image 1 of 3” that had a border that made it look sunken and have it situated on the right hand portion of the screen?
lbl = (root, text=”image 1 of 3”, bd=1, relief=SUNKEN, anchor=E)
What would you put if you wanted to stretch your label across your column span?
status.grid(row=2, column=0, columnspan=3, sticky=W+E)
Remember that columnspan is doesn’t stretch it just adjusts the placement, I believe.
Create a frame that surrounds a button
The only time you can use both frames and grids in your code is when working with frames and what they surround
frame = LabelFrame(root, text”this”, padx=5, pady=5)
frame.pack(padx=10, pady=10)
b = button(frame, text=”button”)
b.grid(column=0, row=0)
Create a variable in a radio button, define what type of variable it is and then retrieve it via a label
r = IntVar()
btn = Radiobutton(root, text=”one”, variable=r, value=1, command=clicked)
def clicked():
lbl = Label(root, text=r.get())
lbl.grid()