├── LICENSE ├── README.md ├── attributes ├── bg_colours.py ├── cursors.py ├── fonts.py ├── padding.py ├── reliefs.py ├── widget_states.py └── width_height.py ├── dialogs ├── color_chooser.py ├── file_dialog.py └── messagebox.py ├── drawing ├── colours.py ├── draw_image.py ├── draw_text.py ├── lines.py ├── shapes.py └── tatras.jpg ├── intro ├── centered_window.py ├── quit_button.py └── simple.py ├── layout ├── absolute.py ├── bardejov.jpg ├── buttons.py ├── calculator.py ├── mincol.jpg ├── review.py ├── rotunda.jpg └── windows.py ├── menustoolbars ├── exit.png ├── popup_menu.py ├── simple_menu.py ├── submenu.py └── toolbar.py ├── snake ├── apple.png ├── dot.png ├── head.png └── snake.py └── widgets ├── check_button.py ├── label.py ├── listbox.py ├── scale.py └── tatras.jpg /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2021, Jan Bodnar 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tkinter-Examples 2 | Code examples from ZetCode's Tkinter tutorial 3 | 4 | https://zetcode.com/tkinter/ 5 | 6 | Tkinter Python Ebook 7 | 8 | https://tkinterpython.top/ 9 | -------------------------------------------------------------------------------- /attributes/bg_colours.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program uses sets various background 7 | colours with bg attribute. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Label 15 | from tkinter import BOTH, LEFT 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Background colours") 28 | self.pack(fill=BOTH) 29 | 30 | frame = Frame(self, borderwidth=10) 31 | frame.pack() 32 | 33 | lbl1 = Label(frame, bg='SlateGray3', width=15, height=10) 34 | lbl1.pack(side=LEFT, padx=3) 35 | 36 | lbl2 = Label(frame, bg='SlateGray4', width=15, height=10) 37 | lbl2.pack(side=LEFT) 38 | 39 | lbl3 = Label(frame, bg='DarkSeaGreen3', width=15, height=10) 40 | lbl3.pack(side=LEFT, padx=3) 41 | 42 | lbl4 = Label(frame, bg='DarkSeaGreen4', width=15, height=10) 43 | lbl4.pack(side=LEFT) 44 | 45 | self.pack() 46 | 47 | def main(): 48 | 49 | root = Tk() 50 | root.geometry("+300+300") 51 | app = Example() 52 | root.mainloop() 53 | 54 | 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /attributes/cursors.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program uses different cursors. 7 | 8 | Author: Jan Bodnar 9 | Last modified: April 2019 10 | Website: www.zetcode.com 11 | """ 12 | 13 | from tkinter import Tk, Frame, Label 14 | from tkinter import BOTH, LEFT 15 | 16 | class Example(Frame): 17 | 18 | def __init__(self): 19 | super().__init__() 20 | 21 | self.initUI() 22 | 23 | 24 | def initUI(self): 25 | 26 | self.master.title("Cursors") 27 | self.pack(fill=BOTH) 28 | 29 | frame = Frame(self, borderwidth=10) 30 | frame.pack() 31 | 32 | lbl1 = Label(frame, bg='SlateGray3', width=15, height=10, 33 | cursor='tcross') 34 | lbl1.pack(side=LEFT, padx=3) 35 | 36 | lbl2 = Label(frame, bg='SlateGray4', width=15, height=10, 37 | cursor='hand2') 38 | lbl2.pack(side=LEFT) 39 | 40 | lbl3 = Label(frame, bg='DarkSeaGreen3', width=15, height=10, 41 | cursor='heart') 42 | lbl3.pack(side=LEFT, padx=3) 43 | 44 | lbl4 = Label(frame, bg='DarkSeaGreen4', width=15, height=10, 45 | cursor='pencil') 46 | lbl4.pack(side=LEFT) 47 | 48 | self.pack() 49 | 50 | def main(): 51 | 52 | root = Tk() 53 | root.geometry("+300+300") 54 | app = Example() 55 | root.mainloop() 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /attributes/fonts.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we display text in three 7 | different fonts. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, BOTH 15 | from tkinter.ttk import Frame, Label, Notebook, Style 16 | 17 | from tkinter.font import Font 18 | 19 | class Example(Frame): 20 | 21 | def __init__(self): 22 | super().__init__() 23 | 24 | self.initUI() 25 | 26 | 27 | def initUI(self): 28 | 29 | self.master.title("Fonts") 30 | self.pack(fill=BOTH, expand=True) 31 | 32 | txt = "Today is a beautiful day" 33 | 34 | myfont = Font(family="Ubuntu Mono", size=16) 35 | label1 = Label(self, text=txt, font=myfont) 36 | label1.grid(row=0, column=0) 37 | 38 | label2 = Label(self, text=txt, font="TkTextFont") 39 | label2.grid(row=1, column=0) 40 | 41 | label3 = Label(self, text=txt, font=('Times', '18', 'italic')) 42 | label3.grid(row=2, column=0) 43 | 44 | 45 | def main(): 46 | 47 | root = Tk() 48 | ex = Example() 49 | root.geometry("+300+300") 50 | root.mainloop() 51 | 52 | 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /attributes/padding.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program uses the padx and pady 7 | widget attributes. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Button 15 | from tkinter import BOTH, LEFT, TOP 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Padding") 28 | self.pack(fill=BOTH) 29 | 30 | frame = Frame(self, bd=5) 31 | frame.pack() 32 | 33 | btn1 = Button(frame, text='Button') 34 | btn1.pack(side=LEFT, padx=5) 35 | 36 | btn2 = Button(frame, text='Button') 37 | btn2.pack(side=LEFT, padx=5) 38 | 39 | frame2 = Frame(self) 40 | frame2.pack() 41 | 42 | btn1 = Button(frame2, text='Button') 43 | btn1.pack(side=TOP, pady=15) 44 | 45 | btn2 = Button(frame2, text='Button') 46 | btn2.pack(side=TOP, pady=15) 47 | 48 | self.pack() 49 | 50 | def main(): 51 | 52 | root = Tk() 53 | root.geometry("300x250+300+300") 54 | app = Example() 55 | root.mainloop() 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /attributes/reliefs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program uses relief styles. 7 | 8 | Author: Jan Bodnar 9 | Last modified: April 2019 10 | Website: www.zetcode.com 11 | """ 12 | 13 | from tkinter import Tk, Frame, Label 14 | from tkinter import BOTH, LEFT, FLAT, SUNKEN, RAISED, GROOVE, RIDGE 15 | 16 | class Example(Frame): 17 | 18 | def __init__(self): 19 | super().__init__() 20 | 21 | self.initUI() 22 | 23 | 24 | def initUI(self): 25 | 26 | self.master.title("Reliefs") 27 | self.pack(fill=BOTH) 28 | 29 | frame = Frame(self, borderwidth=10) 30 | frame.pack() 31 | 32 | lbl1 = Label(frame, bg='LightSteelBlue3', width=15, height=10, relief=FLAT) 33 | lbl1.pack(side=LEFT, padx=3) 34 | 35 | lbl2 = Label(frame, bg='LightSteelBlue3', bd=2, width=15, 36 | height=10, relief=SUNKEN) 37 | lbl2.pack(side=LEFT) 38 | 39 | lbl3 = Label(frame, bg='LightSteelBlue3', bd=2, width=15, 40 | height=10, relief=RAISED) 41 | lbl3.pack(side=LEFT, padx=3) 42 | 43 | lbl4 = Label(frame, bg='LightSteelBlue3', bd=3, width=15, 44 | height=10, relief=GROOVE) 45 | lbl4.pack(side=LEFT) 46 | 47 | lbl5 = Label(frame, bg='LightSteelBlue3', bd=3, width=15, 48 | height=10, relief=RIDGE) 49 | lbl5.pack(side=LEFT, padx=3) 50 | 51 | self.pack() 52 | 53 | def main(): 54 | 55 | root = Tk() 56 | root.geometry("+300+300") 57 | app = Example() 58 | root.mainloop() 59 | 60 | 61 | if __name__ == '__main__': 62 | main() 63 | -------------------------------------------------------------------------------- /attributes/widget_states.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we use the state attribute. 7 | 8 | Author: Jan Bodnar 9 | Last modified: April 2019 10 | Website: www.zetcode.com 11 | """ 12 | 13 | from tkinter import Tk, BOTH, NORMAL, ACTIVE, DISABLED 14 | from tkinter.ttk import Frame, Label 15 | 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Fonts") 28 | self.pack(fill=BOTH, expand=True) 29 | self.columnconfigure(0, pad=5) 30 | self.columnconfigure(1, pad=5) 31 | self.columnconfigure(2, pad=5) 32 | 33 | txt = "Today is a beautiful day" 34 | 35 | label1 = Label(self, text=txt, state=NORMAL) 36 | label1.grid(row=0, column=0) 37 | 38 | label2 = Label(self, text=txt, state=ACTIVE) 39 | label2.grid(row=0, column=1) 40 | 41 | label3 = Label(self, text=txt, state=DISABLED) 42 | label3.grid(row=0, column=2) 43 | 44 | 45 | def main(): 46 | 47 | root = Tk() 48 | ex = Example() 49 | root.geometry("+300+300") 50 | root.mainloop() 51 | 52 | 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /attributes/width_height.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program uses width and height 7 | attributes to set the size of widgets. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Button 15 | from tkinter import BOTH, LEFT 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Width and height") 28 | self.pack(fill=BOTH) 29 | 30 | frame = Frame(self, borderwidth=10) 31 | frame.pack() 32 | 33 | btn1 = Button(frame, text='Button') 34 | btn1.pack(side=LEFT, padx=5) 35 | 36 | btn2 = Button(frame, text='Button', width=8) 37 | btn2.pack(side=LEFT, padx=5) 38 | 39 | btn3 = Button(frame, text='Button', width=5, height=4) 40 | btn3.pack(side=LEFT) 41 | 42 | self.pack() 43 | 44 | def main(): 45 | 46 | root = Tk() 47 | root.geometry("+300+300") 48 | app = Example() 49 | root.mainloop() 50 | 51 | 52 | if __name__ == '__main__': 53 | main() 54 | -------------------------------------------------------------------------------- /dialogs/color_chooser.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we use colorchooser 7 | dialog to change the background of a frame. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Button, BOTH, SUNKEN 15 | from tkinter import colorchooser 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Color chooser") 28 | self.pack(fill=BOTH, expand=1) 29 | 30 | self.btn = Button(self, text="Choose Color", 31 | command=self.onChoose) 32 | self.btn.place(x=30, y=30) 33 | 34 | self.frame = Frame(self, border=1, 35 | relief=SUNKEN, width=100, height=100) 36 | self.frame.place(x=160, y=30) 37 | 38 | 39 | def onChoose(self): 40 | 41 | (rgb, hx) = colorchooser.askcolor() 42 | self.frame.config(bg=hx) 43 | 44 | 45 | def main(): 46 | 47 | root = Tk() 48 | ex = Example() 49 | root.geometry("300x150+300+300") 50 | root.mainloop() 51 | 52 | 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /dialogs/file_dialog.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this program, we use the 7 | tkFileDialog to select a file from 8 | a filesystem. 9 | 10 | Author: Jan Bodnar 11 | Last modified: April 2019 12 | Website: www.zetcode.com 13 | """ 14 | 15 | from tkinter import Frame, Tk, BOTH, Text, Menu, END 16 | from tkinter import filedialog 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("File dialog") 29 | self.pack(fill=BOTH, expand=1) 30 | 31 | menubar = Menu(self.master) 32 | self.master.config(menu=menubar) 33 | 34 | fileMenu = Menu(menubar) 35 | fileMenu.add_command(label="Open", command=self.onOpen) 36 | menubar.add_cascade(label="File", menu=fileMenu) 37 | 38 | self.txt = Text(self) 39 | self.txt.pack(fill=BOTH, expand=1) 40 | 41 | 42 | def onOpen(self): 43 | 44 | ftypes = [('Python files', '*.py'), ('All files', '*')] 45 | dlg = filedialog.Open(self, filetypes = ftypes) 46 | fl = dlg.show() 47 | 48 | if fl != '': 49 | text = self.readFile(fl) 50 | self.txt.insert(END, text) 51 | 52 | 53 | def readFile(self, filename): 54 | 55 | with open(filename, "r") as f: 56 | text = f.read() 57 | 58 | return text 59 | 60 | 61 | def main(): 62 | 63 | root = Tk() 64 | ex = Example() 65 | root.geometry("300x250+300+300") 66 | root.mainloop() 67 | 68 | 69 | if __name__ == '__main__': 70 | main() 71 | -------------------------------------------------------------------------------- /dialogs/messagebox.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this program, we show various 7 | message boxes. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, BOTH 15 | from tkinter.ttk import Frame, Button 16 | from tkinter import messagebox as mbox 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Message boxes") 29 | self.pack() 30 | 31 | error = Button(self, text="Error", command=self.onError) 32 | error.grid(padx=5, pady=5) 33 | warning = Button(self, text="Warning", command=self.onWarn) 34 | warning.grid(row=1, column=0) 35 | question = Button(self, text="Question", command=self.onQuest) 36 | question.grid(row=0, column=1) 37 | inform = Button(self, text="Information", command=self.onInfo) 38 | inform.grid(row=1, column=1) 39 | 40 | 41 | def onError(self): 42 | 43 | mbox.showerror("Error", "Could not open file") 44 | 45 | def onWarn(self): 46 | 47 | mbox.showwarning("Warning", "Deprecated function call") 48 | 49 | def onQuest(self): 50 | 51 | mbox.askquestion("Question", "Are you sure to quit?") 52 | 53 | def onInfo(self): 54 | 55 | mbox.showinfo("Information", "Download completed") 56 | 57 | 58 | def main(): 59 | 60 | root = Tk() 61 | ex = Example() 62 | root.geometry("300x150+300+300") 63 | root.mainloop() 64 | 65 | 66 | if __name__ == '__main__': 67 | main() 68 | -------------------------------------------------------------------------------- /drawing/colours.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program draws three 7 | rectangles filled with different 8 | colours. 9 | 10 | Author: Jan Bodnar 11 | Last modified: April 2019 12 | Website: www.zetcode.com 13 | """ 14 | 15 | from tkinter import Tk, Canvas, Frame, BOTH 16 | 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Colours") 29 | self.pack(fill=BOTH, expand=1) 30 | 31 | canvas = Canvas(self) 32 | canvas.create_rectangle(30, 10, 120, 80, 33 | outline="#fb0", fill="#fb0") 34 | canvas.create_rectangle(150, 10, 240, 80, 35 | outline="#f50", fill="#f50") 36 | canvas.create_rectangle(270, 10, 370, 80, 37 | outline="#05f", fill="#05f") 38 | canvas.pack(fill=BOTH, expand=1) 39 | 40 | 41 | def main(): 42 | 43 | root = Tk() 44 | ex = Example() 45 | root.geometry("400x100+300+300") 46 | root.mainloop() 47 | 48 | 49 | if __name__ == '__main__': 50 | main() 51 | -------------------------------------------------------------------------------- /drawing/draw_image.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we draw an image 7 | on the canvas. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Canvas, Frame, BOTH, NW 15 | from PIL import Image, ImageTk 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("High Tatras") 28 | self.pack(fill=BOTH, expand=1) 29 | 30 | self.img = Image.open("tatras.jpg") 31 | self.tatras = ImageTk.PhotoImage(self.img) 32 | 33 | canvas = Canvas(self, width=self.img.size[0]+20, 34 | height=self.img.size[1]+20) 35 | canvas.create_image(10, 10, anchor=NW, image=self.tatras) 36 | canvas.pack(fill=BOTH, expand=1) 37 | 38 | 39 | def main(): 40 | 41 | root = Tk() 42 | ex = Example() 43 | root.mainloop() 44 | 45 | 46 | if __name__ == '__main__': 47 | main() 48 | -------------------------------------------------------------------------------- /drawing/draw_text.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we draw text 7 | on the window. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Canvas, Frame, BOTH, W 15 | 16 | class Example(Frame): 17 | 18 | def __init__(self): 19 | super().__init__() 20 | 21 | self.initUI() 22 | 23 | 24 | def initUI(self): 25 | 26 | self.master.title("Lyrics") 27 | self.pack(fill=BOTH, expand=1) 28 | 29 | canvas = Canvas(self) 30 | canvas.create_text(20, 30, anchor=W, font="Purisa", 31 | text="Most relationships seem so transitory") 32 | canvas.create_text(20, 60, anchor=W, font="Purisa", 33 | text="They're good but not the permanent one") 34 | canvas.create_text(20, 130, anchor=W, font="Purisa", 35 | text="Who doesn't long for someone to hold") 36 | canvas.create_text(20, 160, anchor=W, font="Purisa", 37 | text="Who knows how to love without being told") 38 | canvas.create_text(20, 190, anchor=W, font="Purisa", 39 | text="Somebody tell me why I'm on my own") 40 | canvas.create_text(20, 220, anchor=W, font="Purisa", 41 | text="If there's a soulmate for everyone") 42 | canvas.pack(fill=BOTH, expand=1) 43 | 44 | 45 | def main(): 46 | 47 | root = Tk() 48 | ex = Example() 49 | root.geometry("420x250+300+300") 50 | root.mainloop() 51 | 52 | 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /drawing/lines.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | The example draws lines on the Canvas. 7 | 8 | Author: Jan Bodnar 9 | Last modified: April 2019 10 | Website: www.zetcode.com 11 | """ 12 | 13 | from tkinter import Tk, Canvas, Frame, BOTH 14 | 15 | class Example(Frame): 16 | 17 | def __init__(self): 18 | super().__init__() 19 | 20 | self.initUI() 21 | 22 | 23 | def initUI(self): 24 | 25 | self.master.title("Lines") 26 | self.pack(fill=BOTH, expand=1) 27 | 28 | canvas = Canvas(self) 29 | canvas.create_line(15, 25, 200, 25) 30 | canvas.create_line(300, 35, 300, 200, dash=(4, 2)) 31 | canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85) 32 | 33 | canvas.pack(fill=BOTH, expand=1) 34 | 35 | 36 | def main(): 37 | 38 | root = Tk() 39 | ex = Example() 40 | root.geometry("400x250+300+300") 41 | root.mainloop() 42 | 43 | 44 | if __name__ == '__main__': 45 | main() 46 | -------------------------------------------------------------------------------- /drawing/shapes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we draw basic 7 | shapes on the canvas. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Canvas, Frame, BOTH 15 | 16 | class Example(Frame): 17 | 18 | def __init__(self): 19 | super().__init__() 20 | 21 | self.initUI() 22 | 23 | 24 | def initUI(self): 25 | 26 | self.master.title("Shapes") 27 | self.pack(fill=BOTH, expand=1) 28 | 29 | canvas = Canvas(self) 30 | canvas.create_oval(10, 10, 80, 80, outline="#f11", 31 | fill="#1f1", width=2) 32 | canvas.create_oval(110, 10, 210, 80, outline="#f11", 33 | fill="#1f1", width=2) 34 | canvas.create_rectangle(230, 10, 290, 60, 35 | outline="#f11", fill="#1f1", width=2) 36 | canvas.create_arc(30, 200, 90, 100, start=0, 37 | extent=210, outline="#f11", fill="#1f1", width=2) 38 | 39 | points = [150, 100, 200, 120, 240, 180, 210, 40 | 200, 150, 150, 100, 200] 41 | canvas.create_polygon(points, outline='#f11', 42 | fill='#1f1', width=2) 43 | 44 | canvas.pack(fill=BOTH, expand=1) 45 | 46 | 47 | def main(): 48 | 49 | root = Tk() 50 | ex = Example() 51 | root.geometry("330x220+300+300") 52 | root.mainloop() 53 | 54 | 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /drawing/tatras.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/drawing/tatras.jpg -------------------------------------------------------------------------------- /intro/centered_window.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This script centers a small 7 | window on the screen. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, BOTH 15 | from tkinter.ttk import Frame 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Centered window") 28 | self.pack(fill=BOTH, expand=1) 29 | self.centerWindow() 30 | 31 | 32 | def centerWindow(self): 33 | 34 | w = 290 35 | h = 150 36 | 37 | sw = self.master.winfo_screenwidth() 38 | sh = self.master.winfo_screenheight() 39 | 40 | x = (sw - w)/2 41 | y = (sh - h)/2 42 | self.master.geometry('%dx%d+%d+%d' % (w, h, x, y)) 43 | 44 | 45 | def main(): 46 | 47 | root = Tk() 48 | ex = Example() 49 | root.mainloop() 50 | 51 | 52 | if __name__ == '__main__': 53 | main() 54 | -------------------------------------------------------------------------------- /intro/quit_button.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program creates a Quit 7 | button. When we press the button, 8 | the application terminates. 9 | 10 | Author: Jan Bodnar 11 | Last modified: April 2019 12 | Website: www.zetcode.com 13 | """ 14 | 15 | from tkinter import Tk, BOTH 16 | from tkinter.ttk import Frame, Button, Style 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.style = Style() 29 | self.style.theme_use("default") 30 | 31 | self.master.title("Quit button") 32 | self.pack(fill=BOTH, expand=1) 33 | 34 | quitButton = Button(self, text="Quit", 35 | command=self.quit) 36 | quitButton.place(x=50, y=50) 37 | 38 | 39 | def main(): 40 | 41 | root = Tk() 42 | root.geometry("250x150+300+300") 43 | app = Example() 44 | root.mainloop() 45 | 46 | 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /intro/simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This script shows a simple window 7 | on the screen. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, BOTH 15 | from tkinter.ttk import Frame 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Simple") 28 | self.pack(fill=BOTH, expand=1) 29 | 30 | 31 | def main(): 32 | 33 | root = Tk() 34 | root.geometry("250x150+300+300") 35 | app = Example() 36 | root.mainloop() 37 | 38 | 39 | if __name__ == '__main__': 40 | main() 41 | -------------------------------------------------------------------------------- /layout/absolute.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we lay out images 7 | using absolute positioning. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from PIL import Image, ImageTk 15 | from tkinter import Tk, BOTH 16 | from tkinter.ttk import Frame, Label, Style 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Absolute positioning") 29 | self.pack(fill=BOTH, expand=1) 30 | 31 | Style().configure("TFrame", background="#333") 32 | 33 | bard = Image.open("bardejov.jpg") 34 | bardejov = ImageTk.PhotoImage(bard) 35 | label1 = Label(self, image=bardejov) 36 | label1.image = bardejov 37 | label1.place(x=20, y=20) 38 | 39 | rot = Image.open("rotunda.jpg") 40 | rotunda = ImageTk.PhotoImage(rot) 41 | label2 = Label(self, image=rotunda) 42 | label2.image = rotunda 43 | label2.place(x=40, y=160) 44 | 45 | minc = Image.open("mincol.jpg") 46 | mincol = ImageTk.PhotoImage(minc) 47 | label3 = Label(self, image=mincol) 48 | label3.image = mincol 49 | label3.place(x=170, y=50) 50 | 51 | 52 | def main(): 53 | 54 | root = Tk() 55 | root.geometry("300x280+300+300") 56 | app = Example() 57 | root.mainloop() 58 | 59 | 60 | if __name__ == '__main__': 61 | main() 62 | -------------------------------------------------------------------------------- /layout/bardejov.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/layout/bardejov.jpg -------------------------------------------------------------------------------- /layout/buttons.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we use the pack manager 7 | to position two buttons in the 8 | bottom-right corner of the window. 9 | 10 | Author: Jan Bodnar 11 | Last modified: April 2019 12 | Website: www.zetcode.com 13 | """ 14 | 15 | from tkinter import Tk, RIGHT, BOTH, RAISED 16 | from tkinter.ttk import Frame, Button, Style 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Buttons") 29 | self.style = Style() 30 | self.style.theme_use("default") 31 | 32 | frame = Frame(self, relief=RAISED, borderwidth=1) 33 | frame.pack(fill=BOTH, expand=True) 34 | 35 | self.pack(fill=BOTH, expand=True) 36 | 37 | closeButton = Button(self, text="Close") 38 | closeButton.pack(side=RIGHT, padx=5, pady=5) 39 | okButton = Button(self, text="OK") 40 | okButton.pack(side=RIGHT) 41 | 42 | 43 | def main(): 44 | 45 | root = Tk() 46 | root.geometry("300x200+300+300") 47 | app = Example() 48 | root.mainloop() 49 | 50 | 51 | if __name__ == '__main__': 52 | main() 53 | -------------------------------------------------------------------------------- /layout/calculator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we use the grid manager 7 | to create a skeleton of a calculator. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, W, E 15 | from tkinter.ttk import Frame, Button, Entry, Style 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Calculator") 28 | 29 | Style().configure("TButton", padding=(0, 5, 0, 5), 30 | font='serif 10') 31 | 32 | self.columnconfigure(0, pad=3) 33 | self.columnconfigure(1, pad=3) 34 | self.columnconfigure(2, pad=3) 35 | self.columnconfigure(3, pad=3) 36 | 37 | self.rowconfigure(0, pad=3) 38 | self.rowconfigure(1, pad=3) 39 | self.rowconfigure(2, pad=3) 40 | self.rowconfigure(3, pad=3) 41 | self.rowconfigure(4, pad=3) 42 | 43 | entry = Entry(self) 44 | entry.grid(row=0, columnspan=4, sticky=W+E) 45 | cls = Button(self, text="Cls") 46 | cls.grid(row=1, column=0) 47 | bck = Button(self, text="Back") 48 | bck.grid(row=1, column=1) 49 | lbl = Button(self) 50 | lbl.grid(row=1, column=2) 51 | clo = Button(self, text="Close") 52 | clo.grid(row=1, column=3) 53 | sev = Button(self, text="7") 54 | sev.grid(row=2, column=0) 55 | eig = Button(self, text="8") 56 | eig.grid(row=2, column=1) 57 | nin = Button(self, text="9") 58 | nin.grid(row=2, column=2) 59 | div = Button(self, text="/") 60 | div.grid(row=2, column=3) 61 | 62 | fou = Button(self, text="4") 63 | fou.grid(row=3, column=0) 64 | fiv = Button(self, text="5") 65 | fiv.grid(row=3, column=1) 66 | six = Button(self, text="6") 67 | six.grid(row=3, column=2) 68 | mul = Button(self, text="*") 69 | mul.grid(row=3, column=3) 70 | 71 | one = Button(self, text="1") 72 | one.grid(row=4, column=0) 73 | two = Button(self, text="2") 74 | two.grid(row=4, column=1) 75 | thr = Button(self, text="3") 76 | thr.grid(row=4, column=2) 77 | mns = Button(self, text="-") 78 | mns.grid(row=4, column=3) 79 | 80 | zer = Button(self, text="0") 81 | zer.grid(row=5, column=0) 82 | dot = Button(self, text=".") 83 | dot.grid(row=5, column=1) 84 | equ = Button(self, text="=") 85 | equ.grid(row=5, column=2) 86 | pls = Button(self, text="+") 87 | pls.grid(row=5, column=3) 88 | 89 | self.pack() 90 | 91 | 92 | def main(): 93 | 94 | root = Tk() 95 | app = Example() 96 | root.mainloop() 97 | 98 | 99 | if __name__ == '__main__': 100 | main() 101 | -------------------------------------------------------------------------------- /layout/mincol.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/layout/mincol.jpg -------------------------------------------------------------------------------- /layout/review.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this example, we use the pack 7 | manager to create a review example. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Text, TOP, BOTH, X, N, LEFT 15 | from tkinter.ttk import Frame, Label, Entry 16 | 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Review") 29 | self.pack(fill=BOTH, expand=True) 30 | 31 | frame1 = Frame(self) 32 | frame1.pack(fill=X) 33 | 34 | lbl1 = Label(frame1, text="Title", width=6) 35 | lbl1.pack(side=LEFT, padx=5, pady=5) 36 | 37 | entry1 = Entry(frame1) 38 | entry1.pack(fill=X, padx=5, expand=True) 39 | 40 | frame2 = Frame(self) 41 | frame2.pack(fill=X) 42 | 43 | lbl2 = Label(frame2, text="Author", width=6) 44 | lbl2.pack(side=LEFT, padx=5, pady=5) 45 | 46 | entry2 = Entry(frame2) 47 | entry2.pack(fill=X, padx=5, expand=True) 48 | 49 | frame3 = Frame(self) 50 | frame3.pack(fill=BOTH, expand=True) 51 | 52 | lbl3 = Label(frame3, text="Review", width=6) 53 | lbl3.pack(side=LEFT, anchor=N, padx=5, pady=5) 54 | 55 | txt = Text(frame3) 56 | txt.pack(fill=BOTH, pady=5, padx=5, expand=True) 57 | 58 | 59 | def main(): 60 | 61 | root = Tk() 62 | root.geometry("300x300+300+300") 63 | app = Example() 64 | root.mainloop() 65 | 66 | 67 | if __name__ == '__main__': 68 | main() 69 | -------------------------------------------------------------------------------- /layout/rotunda.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/layout/rotunda.jpg -------------------------------------------------------------------------------- /layout/windows.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we use the grid 7 | manager to create a more complicated Windows 8 | layout. 9 | 10 | Author: Jan Bodnar 11 | Last modified: April 2019 12 | Website: www.zetcode.com 13 | """ 14 | 15 | from tkinter import Tk, Text, BOTH, W, N, E, S 16 | from tkinter.ttk import Frame, Button, Label, Style 17 | 18 | 19 | class Example(Frame): 20 | 21 | def __init__(self): 22 | super().__init__() 23 | 24 | self.initUI() 25 | 26 | 27 | def initUI(self): 28 | 29 | self.master.title("Windows") 30 | self.pack(fill=BOTH, expand=True) 31 | 32 | self.columnconfigure(1, weight=1) 33 | self.columnconfigure(3, pad=7) 34 | self.rowconfigure(3, weight=1) 35 | self.rowconfigure(5, pad=7) 36 | 37 | lbl = Label(self, text="Windows") 38 | lbl.grid(sticky=W, pady=4, padx=5) 39 | 40 | area = Text(self) 41 | area.grid(row=1, column=0, columnspan=2, rowspan=4, 42 | padx=5, sticky=E+W+S+N) 43 | 44 | abtn = Button(self, text="Activate") 45 | abtn.grid(row=1, column=3) 46 | 47 | cbtn = Button(self, text="Close") 48 | cbtn.grid(row=2, column=3, pady=4) 49 | 50 | hbtn = Button(self, text="Help") 51 | hbtn.grid(row=5, column=0, padx=5) 52 | 53 | obtn = Button(self, text="OK") 54 | obtn.grid(row=5, column=3) 55 | 56 | 57 | def main(): 58 | 59 | root = Tk() 60 | root.geometry("350x300+300+300") 61 | app = Example() 62 | root.mainloop() 63 | 64 | 65 | if __name__ == '__main__': 66 | main() 67 | -------------------------------------------------------------------------------- /menustoolbars/exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/menustoolbars/exit.png -------------------------------------------------------------------------------- /menustoolbars/popup_menu.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this program, we create 7 | a popup menu. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Menu 15 | 16 | class Example(Frame): 17 | 18 | def __init__(self): 19 | super().__init__() 20 | 21 | self.initUI() 22 | 23 | 24 | def initUI(self): 25 | 26 | self.master.title("Popup menu") 27 | self.menu = Menu(self.master, tearoff=0) 28 | self.menu.add_command(label="Beep", command=self.bell) 29 | self.menu.add_command(label="Exit", command=self.onExit) 30 | 31 | self.master.bind("", self.showMenu) 32 | self.pack() 33 | 34 | 35 | def showMenu(self, e): 36 | 37 | self.menu.post(e.x_root, e.y_root) 38 | 39 | 40 | def onExit(self): 41 | 42 | self.quit() 43 | 44 | 45 | def main(): 46 | 47 | root = Tk() 48 | root.geometry("250x150+300+300") 49 | app = Example() 50 | root.mainloop() 51 | 52 | 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /menustoolbars/simple_menu.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program shows a simple 7 | menu. It has one action, which 8 | will terminate the program, when 9 | selected. 10 | 11 | Author: Jan Bodnar 12 | Last modified: April 2019 13 | Website: www.zetcode.com 14 | """ 15 | 16 | from tkinter import Tk, Frame, Menu 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Simple menu") 29 | 30 | menubar = Menu(self.master) 31 | self.master.config(menu=menubar) 32 | 33 | fileMenu = Menu(menubar) 34 | fileMenu.add_command(label="Exit", command=self.onExit) 35 | menubar.add_cascade(label="File", menu=fileMenu) 36 | 37 | 38 | def onExit(self): 39 | 40 | self.quit() 41 | 42 | 43 | def main(): 44 | 45 | root = Tk() 46 | root.geometry("250x150+300+300") 47 | app = Example() 48 | root.mainloop() 49 | 50 | 51 | if __name__ == '__main__': 52 | main() 53 | -------------------------------------------------------------------------------- /menustoolbars/submenu.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script we create a submenu 7 | a separator and keyboard shortcuts to menus. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Menu 15 | 16 | class Example(Frame): 17 | 18 | def __init__(self): 19 | super().__init__() 20 | 21 | self.initUI() 22 | 23 | 24 | def initUI(self): 25 | 26 | self.master.title("Submenu") 27 | 28 | menubar = Menu(self.master) 29 | self.master.config(menu=menubar) 30 | 31 | fileMenu = Menu(menubar) 32 | 33 | submenu = Menu(fileMenu) 34 | submenu.add_command(label="New feed") 35 | submenu.add_command(label="Bookmarks") 36 | submenu.add_command(label="Mail") 37 | fileMenu.add_cascade(label='Import', menu=submenu, underline=0) 38 | 39 | fileMenu.add_separator() 40 | 41 | fileMenu.add_command(label="Exit", underline=0, command=self.onExit) 42 | menubar.add_cascade(label="File", underline=0, menu=fileMenu) 43 | 44 | 45 | def onExit(self): 46 | 47 | self.quit() 48 | 49 | 50 | def main(): 51 | 52 | root = Tk() 53 | root.geometry("250x150+300+300") 54 | app = Example() 55 | root.mainloop() 56 | 57 | 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /menustoolbars/toolbar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this program, we create a toolbar. 7 | 8 | Author: Jan Bodnar 9 | Last modified: April 2019 10 | Website: www.zetcode.com 11 | """ 12 | 13 | from PIL import Image, ImageTk 14 | from tkinter import Tk, Frame, Menu, Button 15 | from tkinter import LEFT, TOP, X, FLAT, RAISED 16 | 17 | 18 | class Example(Frame): 19 | 20 | def __init__(self): 21 | super().__init__() 22 | 23 | self.initUI() 24 | 25 | 26 | def initUI(self): 27 | 28 | self.master.title("Toolbar") 29 | 30 | menubar = Menu(self.master) 31 | self.fileMenu = Menu(self.master, tearoff=0) 32 | self.fileMenu.add_command(label="Exit", command=self.onExit) 33 | menubar.add_cascade(label="File", menu=self.fileMenu) 34 | 35 | toolbar = Frame(self.master, bd=1, relief=RAISED) 36 | 37 | self.img = Image.open("exit.png") 38 | eimg = ImageTk.PhotoImage(self.img) 39 | 40 | exitButton = Button(toolbar, image=eimg, relief=FLAT, 41 | command=self.quit) 42 | exitButton.image = eimg 43 | exitButton.pack(side=LEFT, padx=2, pady=2) 44 | 45 | toolbar.pack(side=TOP, fill=X) 46 | self.master.config(menu=menubar) 47 | self.pack() 48 | 49 | 50 | def onExit(self): 51 | self.quit() 52 | 53 | 54 | def main(): 55 | 56 | root = Tk() 57 | root.geometry("250x150+300+300") 58 | app = Example() 59 | root.mainloop() 60 | 61 | 62 | if __name__ == '__main__': 63 | main() 64 | -------------------------------------------------------------------------------- /snake/apple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/snake/apple.png -------------------------------------------------------------------------------- /snake/dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/snake/dot.png -------------------------------------------------------------------------------- /snake/head.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/snake/head.png -------------------------------------------------------------------------------- /snake/snake.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This is a simple Snake game 7 | clone. 8 | 9 | Author: Jan Bodnar 10 | Website: zetcode.com 11 | Last edited: April 2019 12 | """ 13 | 14 | import sys 15 | import random 16 | from PIL import Image, ImageTk 17 | from tkinter import Tk, Frame, Canvas, ALL, NW 18 | 19 | class Cons: 20 | 21 | BOARD_WIDTH = 300 22 | BOARD_HEIGHT = 300 23 | DELAY = 100 24 | DOT_SIZE = 10 25 | MAX_RAND_POS = 27 26 | 27 | class Board(Canvas): 28 | 29 | def __init__(self): 30 | super().__init__(width=Cons.BOARD_WIDTH, height=Cons.BOARD_HEIGHT, 31 | background="black", highlightthickness=0) 32 | 33 | self.initGame() 34 | self.pack() 35 | 36 | 37 | def initGame(self): 38 | '''initializes game''' 39 | 40 | self.inGame = True 41 | self.dots = 3 42 | self.score = 0 43 | 44 | # variables used to move snake object 45 | self.moveX = Cons.DOT_SIZE 46 | self.moveY = 0 47 | 48 | # starting apple coordinates 49 | self.appleX = 100 50 | self.appleY = 190 51 | 52 | self.loadImages() 53 | 54 | self.createObjects() 55 | self.locateApple() 56 | self.bind_all("", self.onKeyPressed) 57 | self.after(Cons.DELAY, self.onTimer) 58 | 59 | 60 | def loadImages(self): 61 | '''loads images from the disk''' 62 | 63 | try: 64 | self.idot = Image.open("dot.png") 65 | self.dot = ImageTk.PhotoImage(self.idot) 66 | self.ihead = Image.open("head.png") 67 | self.head = ImageTk.PhotoImage(self.ihead) 68 | self.iapple = Image.open("apple.png") 69 | self.apple = ImageTk.PhotoImage(self.iapple) 70 | 71 | except IOError as e: 72 | 73 | print(e) 74 | sys.exit(1) 75 | 76 | 77 | def createObjects(self): 78 | '''creates objects on Canvas''' 79 | 80 | self.create_text(30, 10, text="Score: {0}".format(self.score), 81 | tag="score", fill="white") 82 | self.create_image(self.appleX, self.appleY, image=self.apple, 83 | anchor=NW, tag="apple") 84 | self.create_image(50, 50, image=self.head, anchor=NW, tag="head") 85 | self.create_image(30, 50, image=self.dot, anchor=NW, tag="dot") 86 | self.create_image(40, 50, image=self.dot, anchor=NW, tag="dot") 87 | 88 | 89 | def checkAppleCollision(self): 90 | '''checks if the head of snake collides with apple''' 91 | 92 | apple = self.find_withtag("apple") 93 | head = self.find_withtag("head") 94 | 95 | x1, y1, x2, y2 = self.bbox(head) 96 | overlap = self.find_overlapping(x1, y1, x2, y2) 97 | 98 | for ovr in overlap: 99 | 100 | if apple[0] == ovr: 101 | 102 | self.score += 1 103 | x, y = self.coords(apple) 104 | self.create_image(x, y, image=self.dot, anchor=NW, tag="dot") 105 | self.locateApple() 106 | 107 | 108 | def moveSnake(self): 109 | '''moves the Snake object''' 110 | 111 | dots = self.find_withtag("dot") 112 | head = self.find_withtag("head") 113 | 114 | items = dots + head 115 | 116 | z = 0 117 | while z < len(items)-1: 118 | 119 | c1 = self.coords(items[z]) 120 | c2 = self.coords(items[z+1]) 121 | self.move(items[z], c2[0]-c1[0], c2[1]-c1[1]) 122 | z += 1 123 | 124 | self.move(head, self.moveX, self.moveY) 125 | 126 | 127 | def checkCollisions(self): 128 | '''checks for collisions''' 129 | 130 | dots = self.find_withtag("dot") 131 | head = self.find_withtag("head") 132 | 133 | x1, y1, x2, y2 = self.bbox(head) 134 | overlap = self.find_overlapping(x1, y1, x2, y2) 135 | 136 | for dot in dots: 137 | for over in overlap: 138 | if over == dot: 139 | self.inGame = False 140 | 141 | if x1 < 0: 142 | self.inGame = False 143 | 144 | if x1 > Cons.BOARD_WIDTH - Cons.DOT_SIZE: 145 | self.inGame = False 146 | 147 | if y1 < 0: 148 | self.inGame = False 149 | 150 | if y1 > Cons.BOARD_HEIGHT - Cons.DOT_SIZE: 151 | self.inGame = False 152 | 153 | 154 | def locateApple(self): 155 | '''places the apple object on Canvas''' 156 | 157 | apple = self.find_withtag("apple") 158 | self.delete(apple[0]) 159 | 160 | r = random.randint(0, Cons.MAX_RAND_POS) 161 | self.appleX = r * Cons.DOT_SIZE 162 | r = random.randint(0, Cons.MAX_RAND_POS) 163 | self.appleY = r * Cons.DOT_SIZE 164 | 165 | self.create_image(self.appleX, self.appleY, anchor=NW, 166 | image=self.apple, tag="apple") 167 | 168 | 169 | def onKeyPressed(self, e): 170 | '''controls direction variables with cursor keys''' 171 | 172 | key = e.keysym 173 | 174 | LEFT_CURSOR_KEY = "Left" 175 | if key == LEFT_CURSOR_KEY and self.moveX <= 0: 176 | 177 | self.moveX = -Cons.DOT_SIZE 178 | self.moveY = 0 179 | 180 | RIGHT_CURSOR_KEY = "Right" 181 | if key == RIGHT_CURSOR_KEY and self.moveX >= 0: 182 | 183 | self.moveX = Cons.DOT_SIZE 184 | self.moveY = 0 185 | 186 | RIGHT_CURSOR_KEY = "Up" 187 | if key == RIGHT_CURSOR_KEY and self.moveY <= 0: 188 | 189 | self.moveX = 0 190 | self.moveY = -Cons.DOT_SIZE 191 | 192 | DOWN_CURSOR_KEY = "Down" 193 | if key == DOWN_CURSOR_KEY and self.moveY >= 0: 194 | 195 | self.moveX = 0 196 | self.moveY = Cons.DOT_SIZE 197 | 198 | 199 | def onTimer(self): 200 | '''creates a game cycle each timer event''' 201 | 202 | self.drawScore() 203 | self.checkCollisions() 204 | 205 | if self.inGame: 206 | self.checkAppleCollision() 207 | self.moveSnake() 208 | self.after(Cons.DELAY, self.onTimer) 209 | else: 210 | self.gameOver() 211 | 212 | 213 | def drawScore(self): 214 | '''draws score''' 215 | 216 | score = self.find_withtag("score") 217 | self.itemconfigure(score, text="Score: {0}".format(self.score)) 218 | 219 | 220 | def gameOver(self): 221 | '''deletes all objects and draws game over message''' 222 | 223 | self.delete(ALL) 224 | self.create_text(self.winfo_width() /2, self.winfo_height()/2, 225 | text="Game Over with score {0}".format(self.score), fill="white") 226 | 227 | 228 | class Snake(Frame): 229 | 230 | def __init__(self): 231 | super().__init__() 232 | 233 | self.master.title('Snake') 234 | self.board = Board() 235 | self.pack() 236 | 237 | 238 | def main(): 239 | 240 | root = Tk() 241 | nib = Snake() 242 | root.mainloop() 243 | 244 | 245 | if __name__ == '__main__': 246 | main() 247 | -------------------------------------------------------------------------------- /widgets/check_button.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | This program toggles the title of the 7 | window with the Checkbutton widget. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, Frame, Checkbutton 15 | from tkinter import BooleanVar, BOTH 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Checkbutton") 28 | 29 | self.pack(fill=BOTH, expand=True) 30 | self.var = BooleanVar() 31 | 32 | cb = Checkbutton(self, text="Show title", 33 | variable=self.var, command=self.onClick) 34 | cb.select() 35 | cb.place(x=50, y=50) 36 | 37 | 38 | def onClick(self): 39 | 40 | if self.var.get() == True: 41 | self.master.title("Checkbutton") 42 | else: 43 | self.master.title("") 44 | 45 | 46 | def main(): 47 | 48 | root = Tk() 49 | root.geometry("250x150+300+300") 50 | app = Example() 51 | root.mainloop() 52 | 53 | 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /widgets/label.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we use the Label 7 | widget to show an image. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from PIL import Image, ImageTk 15 | from tkinter import Tk 16 | from tkinter.ttk import Frame, Label 17 | import sys 18 | 19 | class Example(Frame): 20 | 21 | def __init__(self): 22 | super().__init__() 23 | 24 | self.loadImage() 25 | self.initUI() 26 | 27 | 28 | def loadImage(self): 29 | try: 30 | self.img = Image.open("tatras.jpg") 31 | 32 | except IOError: 33 | print("Unable to load image") 34 | sys.exit(1) 35 | 36 | 37 | def initUI(self): 38 | 39 | self.master.title("Label") 40 | 41 | tatras = ImageTk.PhotoImage(self.img) 42 | label = Label(self, image=tatras) 43 | 44 | # reference must be stored 45 | label.image = tatras 46 | 47 | label.pack() 48 | self.pack() 49 | 50 | 51 | def setGeometry(self): 52 | 53 | w, h = self.img.size 54 | self.master.geometry(("%dx%d+300+300") % (w, h)) 55 | 56 | 57 | def main(): 58 | 59 | root = Tk() 60 | ex = Example() 61 | ex.setGeometry() 62 | root.mainloop() 63 | 64 | 65 | if __name__ == '__main__': 66 | main() 67 | -------------------------------------------------------------------------------- /widgets/listbox.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we show how to 7 | use the Listbox widget. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, BOTH, Listbox, StringVar, END 15 | from tkinter.ttk import Frame, Label 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Listbox") 28 | 29 | self.pack(fill=BOTH, expand=1) 30 | 31 | acts = ['Scarlett Johansson', 'Rachel Weiss', 32 | 'Natalie Portman', 'Jessica Alba'] 33 | 34 | lb = Listbox(self) 35 | 36 | for i in acts: 37 | lb.insert(END, i) 38 | 39 | lb.bind("<>", self.onSelect) 40 | 41 | lb.pack(pady=15) 42 | 43 | self.var = StringVar() 44 | self.label = Label(self, text=0, textvariable=self.var) 45 | self.label.pack() 46 | 47 | 48 | def onSelect(self, val): 49 | 50 | sender = val.widget 51 | idx = sender.curselection() 52 | value = sender.get(idx) 53 | 54 | self.var.set(value) 55 | 56 | 57 | def main(): 58 | 59 | root = Tk() 60 | ex = Example() 61 | root.geometry("300x250+300+300") 62 | root.mainloop() 63 | 64 | 65 | if __name__ == '__main__': 66 | main() 67 | -------------------------------------------------------------------------------- /widgets/scale.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | ZetCode Tkinter tutorial 5 | 6 | In this script, we show how to 7 | use the Scale widget. 8 | 9 | Author: Jan Bodnar 10 | Last modified: April 2019 11 | Website: www.zetcode.com 12 | """ 13 | 14 | from tkinter import Tk, BOTH, IntVar, LEFT 15 | from tkinter.ttk import Frame, Label, Scale, Style 16 | 17 | class Example(Frame): 18 | 19 | def __init__(self): 20 | super().__init__() 21 | 22 | self.initUI() 23 | 24 | 25 | def initUI(self): 26 | 27 | self.master.title("Scale") 28 | self.style = Style() 29 | self.style.theme_use("default") 30 | 31 | self.pack(fill=BOTH, expand=1) 32 | 33 | scale = Scale(self, from_=0, to=100, 34 | command=self.onScale) 35 | scale.pack(side=LEFT, padx=15) 36 | 37 | self.var = IntVar() 38 | self.label = Label(self, text=0, textvariable=self.var) 39 | self.label.pack(side=LEFT) 40 | 41 | 42 | def onScale(self, val): 43 | 44 | v = int(float(val)) 45 | self.var.set(v) 46 | 47 | 48 | def main(): 49 | 50 | root = Tk() 51 | ex = Example() 52 | root.geometry("250x100+300+300") 53 | root.mainloop() 54 | 55 | 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /widgets/tatras.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/janbodnar/Tkinter-Examples/268e0d2b7b849fbe15a63d4bdb1fc72938141806/widgets/tatras.jpg --------------------------------------------------------------------------------