4/9/2018 All the pages that follow this one this one have more lengthy demos that were posted first, but what you will find below is special. Shortly, wikipython will announce an awesome real-time resource for students and occasional users of tkinter. A completely free, open source, uncomplicated python program called tksidekick. The third version is in the hands of beta testers now. It will be explained here on wikipython but just like our toolboxes you will download the program from GitHub for safety. By the way – just common sense – you should never run a program you download without having your anti-virus check it out.
You run tksidekick “on the side” while you are writing tkinter code and, for the most part, it pulls information out of your system about widgets and commands and objects of any sort. Click on a button that says “Listbox”, for example, and it instantly shows all the Options and their attributes (viewed 3 ways) and all the methods that apply to that widget. You can quickly see all the wifo commands, the window manager command, the constants in your system, and even run color chooser which will leave behind the color formula you need. You can search for any type of Python or tkinter object two ways. You can see all the pydoc topics, select and read the ones you choose.
The biggest thing not pulled directly is a short code segment for each widget that shows an example of the widget being used – instantiated. (tksidekick requires Python 3.6 because it uses f strings for list formatting.) These “messy” segments are kept in a dictionary and have been carefully constructed and tested. They are presented when/if you ask for them. These segments assume a header that sets up a tkinter workspace and an ending mainloop(). We suggest what we use which you will find at “ARITCLES RANTS ETC.” -> “2 Standard GUI Test Setups”.
Here they are, reformatted a bit for display in the context of this website:
Toplevel :
#Toplevel - a primary level widget to hold and manage all the rest top1 = Toplevel(root, bg="light blue") top1.title("Top 1 - Workwindow") top1.attributes("-topmost", 1) # make sure top1 is on top to start root.update() # but don't leave it locked in place top1.attributes("-topmost", 0) # in case you use lower or lift
Button :
#Button - generally used to trigger an action with a single mouse click def callback(): root.destroy() b1=Button(top1, text="Button #1", command=callback) b1.pack(ipadx=5, ipady=5)
Canvas :
#Canvas - display graphic objects cv1 = Canvas(top1, width=250, height=200, bg="beige") cv1.pack() cv1.create_line(0, 0, 200, 100) cv1.create_line(0, 100, 200, 0, fill="red", dash=(4, 4)) cv1.create_rectangle(50, 25, 150, 75, fill="blue") #Traditional Effbot example of lines and rectangle
Checkbutton :
#Checkbutton - checkboxes that share a common variable var1=IntVar() var1.set(0) def cb1callback(): cb1.config(bg="white") cb2.config(bg="white") eval("cb" + str(var1.get())).config(bg="red") cb1=Checkbutton(top1, text="Choise #1", variable=var1, onvalue=1, command=cb1callback) cb2=Checkbutton(top1, text="Choise #2", variable=var1, onvalue=2, command=cb1callback) cb1.pack() cb2.pack()
Entry :
#Entry - display text or accept input of text e1_txt = StringVar() e1_txt.set("") l1=Label(top1) # display Entry input in a label after <Return> def e1_callback(self): l1.config(text=e1_txt.get()) l1.pack() e1_txt.set("") e1=Entry(top1, width=60, textvariable=e1_txt, relief=GROOVE, bd=10) e1.focus_set() e1.bind("<Return>", e1_callback) e1.pack()
Frame :
#Frame - holds other widgets, can be a spacer var1=IntVar() var1.set(0) f1=Frame(top1, relief=RAISED, bd=10) def cb1callback(): cb1.config(bg="white") cb2.config(bg="white") eval("cb" + str(var1.get())).config(bg="red") cb1=Checkbutton(f1, text="Choise #1", variable=var1, onvalue=1, command=cb1callback) cb2=Checkbutton(f1, text="Choise #2", variable=var1, onvalue=2, command=cb1callback) cb1.pack() cb2.pack() f1.pack()
LabelFrame :
#LableFrame - holds other widgets with frame and title lf1=LabelFrame(top1, text="A LabelFrame", relief=GROOVE, width=40,bd=10,padx=10, pady=10) lf1.pack() l1=Label(lf1, text="A label inside a labelframe.", bg="misty rose") l1.pack()
Label:
#Label - a text message or a lable - semi-permanent lf1=LabelFrame(top1, text="A LabelFrame", relief=GROOVE, width=40,bd=5,padx=8, pady=8) lf1.pack() l1=Label(lf1, text="A label inside a labelframe.", relief=SUNKEN, bd=10, bg="red") l1.pack()
Listbox :
#Listbox - possibly a box to hold a list alist = ["Hotdogs","Hamburgers","Chips","Soda"] var1 = StringVar() var1.set(alist) lb=Listbox(top1, listvariable=var1, relief=RAISED, bd=15, font=("Times", 14, "bold")) lb.config(bg='grey75', width=15) lb.pack()
Menu:
# Menu - Program options and choices from tkinter import messagebox def menucallback(): messagebox.showinfo("Menu callback","A call back summons a note or a feature.", parent=top1) mainbar = Menu(top1, tearoff=0) # create the main bar top1.configure(menu=mainbar) # and connect toplevel and menu mainbar.add_command(label="Exit", command=lambda:root.destroy()) # add a single command # or add pulldowns (just one here as example) called cascades cascade1Menu = Menu(mainbar, tearoff=0, relief=GROOVE) cascade1Menu.add_command(label="Callback demo", command=lambda: menucallback()) cascade1Menu.add_command(label="Other exit", command=lambda: root.destroy()) mainbar.add_cascade(label="Cascade demo", menu=cascade1Menu)
Menubutton :
# Menubutton # Menubutton is obsolete - use Menu instead. # An example of Menubutton can be found on www.wikipython.com
Message :
#Message m_txt = "Red Leicester: Im afreaid we are fresh out of Red Leicester sir.\nTilsit: \nNever\ at the end of the week, sir. \nAlways get it fresh first thing on Maonday.\n" msg=Message(top1, bg="wheat1", text=m_txt, relief=GROOVE, font="Georgia 14", width="2i") msg.pack(padx=50, pady=50)
PanedWindow :
#PanedWindow - create divisions with user moveable partition pw1 = PanedWindow(top1, orient=VERTICAL) pw1.pack(fill=BOTH, expand=1) ltop = Label(pw1, text="top pane", bg="grey70",height=20) pw1.add(ltop) lbottom = Label(pw1, text="bottom pane", bg="misty rose", height=20) pw1.add(lbottom)
Radiobutton :
#Radiobutton: def rbchange(): l1.config(text= str(rbintvar.get())) rbintvar = IntVar() rb1 = Radiobutton(top1, text="Un", width=7, variable=rbintvar, value=1, command=rbchange) rb2 = Radiobutton(top1, text="Deux", width=7, variable=rbintvar, value=2, command=rbchange) rb3 = Radiobutton(top1, text="Trois",width=7, variable=rbintvar, value=3, command=rbchange) rb1.pack() rb2.pack() rb3.pack() l1=Label(top1, text="?", width=9, bg="yellow") l1.pack()
Scale :
#Scale - see Anatomy of a Scale on www.wikipython.com def scaletest(self): l1.configure(text=myscalevalue.get()) myscalevalue = IntVar() myscalevalue.set(125) # in options note "from_" not "from" myscale = Scale(top1, activebackground="orangered", from_=0, to=255, length="3i") myscale.config(label="Tester", tickinterval=50, width=100, command=scaletest, variable=myscalevalue) myscale.pack() l1=Label(top1, width=25, bg="light green") l1.pack(pady=30)
Scrollbar :
#Scrollbar t1text="nonsense on to of "*500 #create some filler text # NOTE CAREFULLY the ORDER in which the LabelFrame, Scrollbar and # Text widget are CREATED AND PACKED - does not work any other way! lf1=LabelFrame(top1, width=225, height=768) #create a LableFrame object -DONT pack sbar1 = Scrollbar(lf1) #create a scrollbar and pack in LabelFrame on right sbar1.pack(side="right", fill="y")#create a text and pack in LabelFrame on left, fill both t1=Text(lf1, width=40, height=30, wrap=CHAR, yscrollcommand=sbar1.set) t1.pack(side="left", fill="both", expand=True) #pack it in lf1 t1.insert(END, t1text) # then fill it up sbar1.config(command=t1.yview) #now connect scrollbar and text frame lf1.grid(row=0, column=0) # when all that is done, grid the LabelFrame # any other sequence voids propagate lf1.pack_propagate(False) #without propagate the text frame expands
Spinbox :
#Spinbox sb1value=IntVar() sb1value.set(1) def sbaction(): l1.config(text=sb1value.get()) sb1=Spinbox(top1, textvariable=sb1value, values=(1,2,3,4,5), width=20, wrap=True) sb1.configure(activebackground="light blue", bg="beige", bd=10, command=sbaction, buttonbackground="LightGoldenrod2", fg="black", justify="center", repeatinterval=500, width=2) sb1.pack(padx=300, pady=200, ipadx=40, ipady=15) l1=Label(top1, text=sb1value.get(), width=20, bg="gray80") l1.pack()
Text :
#Text ttext="nonsense on to of "*100 tx1=Text(top1, width=30, height = 40, relief=RAISED, bd=10) tx1.pack() tx1.delete(1.0, END) # clear the box tx1.insert(END, ttext) # insert new text