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)