#Import tkinter gui from tkinter import * from tkinter import ttk from sympy.physics.quantum.circuitplot import labeller #obtain data from user x=str(input("Please enter your name: ")) #general object root = Tk() #LABEL GENERATION Label(root, text=x).pack() #general object root.mainloop() ------------------------------------------ #Creating widget and change test #Adding label with ttk.label method another method from tkinter import * from tkinter import ttk root = Tk() #creates a top level window button = ttk.Button(root, text = 'Click Me') #create widget like button button.pack() #create widget button.config(text = 'Push Me') #Revise widget button object ttk.Label(root, text ='Hello, Tkinter!').pack() #Generating label #Define a button and attach command ttk.Button(master, text = "0", command = 0).grid(row = 7, column = 1) root.mainloop()# mainloop() add ------------------------------------------- #Generating through root area logo = PhotoImage(file = 'python_logo.gif') # change path to image as necessary label.grid(row = 0, column = 0, columnspan = 2) label.img = logo #to make imagine able to appear out of function label.config(image = label.img) -------------------------------------------- #Generate and adjust label label.config(justify = CENTER) #adjusting through center label.config(foreground = 'blue', background = 'yellow') label.config(font = ('Courier', 18, 'bold')) label.config(text = 'Howdy, Tkinter!') -------------------------------------------- #Generate and adjust LOGO logo = PhotoImage(file = 'python_logo.gif') # change path to image as necessary button.config(image = logo, compound = LEFT) small_logo = logo.subsample(5, 5) button.config(image = small_logo) button.state(['!disabled']) # make unable button -------------------------------------------- #Call back button to answer button = ttk.Button(root, text = "Click Me") #creating button button.pack() def callback(): print('Clicked!') button.config(command = callback) #method -------------------------------------------- #checkbutton checkbutton = ttk.Checkbutton(root, text = "Topic") checkbutton.pack() spam = StringVar() checkbutton.config(variable = spam) #radiobutton ttk.Radiobutton(root, text = 'Eggs', variable = spam, value = 'Eggs').pack() print(spam.get()) -------------------------------------------- #Entry window, text window entry = ttk.Entry(root, width = 30) entry.pack() entry.get() entry.delete(0, 1) entry.delete(0, END) entry.insert(0, 'Enter your password') entry.config(show = '*') -------------------------------------------- #Combobox and spinbox month = StringVar() combobox = ttk.Combobox(root, textvariable = month) combobox.pack() combobox.config(values = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) print(month.get()) month.set('Select month!') year = StringVar() Spinbox(root, from_ = 1990, to = 2014, textvariable = year).pack() print(year.get()) x=year.get() -------------------------------------------- #progressbar scale progressbar = ttk.Progressbar(root, orient = HORIZONTAL, length = 200) progressbar.pack() progressbar.config(mode = 'indeterminate') progressbar.start() progressbar.stop() progressbar.config(mode = 'determinate', maximum = 11.0, value = 4.2) progressbar.config(value = 8.0) progressbar.step() progressbar.step(5) value = DoubleVar() progressbar.config(variable = value) scale = ttk.Scale(root, orient = HORIZONTAL, length = 200, variable = value, from_ = 0.0, to = 11.0) scale.pack() scale.set(4.2) print(scale.get()) -------------------------------------------- #Frame generator frame = ttk.Frame(root) frame.pack() frame.config(height = 100, width = 200) frame.config(relief = RIDGE) ttk.Button(frame, text = 'Click Me').pack() frame.config(padding = (30, 15)) ttk.LabelFrame(root, height = 100, width = 200, text = 'My Frame').pack() ------------------------------------------- #Generate new window window = Toplevel(root) window.title('New Window') window.lower() window.lift(root) window.state('zoomed') window.state('withdrawn') window.state('iconic') window.state('normal') print(window.state()) window.state('normal') window.iconify() window.deiconify() window.geometry('640x480+50+100') print(window.geometry()) -------------------------------------------- #Resizable panel and Title root.Tk() root.title('Explore California Feedback') root.resizable(False, False) root.configure(background = '#e1d8b9') -------------------------------------------- #Paned Window panedwindow = ttk.Panedwindow(root, orient = HORIZONTAL) panedwindow.pack(fill = BOTH, expand = True) frame1 = ttk.Frame(panedwindow, width = 100, height = 300, relief = SUNKEN) frame2 = ttk.Frame(panedwindow, width = 400, height = 400, relief = SUNKEN) frame3 = ttk.Frame(panedwindow, width = 50, height = 50, relief = SUNKEN) panedwindow.add(frame1, weight = 1) panedwindow.add(frame2, weight = 4) panedwindow.insert(1, frame3) #panedwindow.forget(1) -------------------------------------------- #Notebookwith various pages notebook = ttk.Notebook(root) notebook.pack() frame1 = ttk.Frame(notebook) frame2 = ttk.Frame(notebook) notebook.add(frame1, text = 'One') notebook.add(frame2, text = 'Two') ttk.Button(frame1, text = 'Click Me').pack() -------------------------------------------- #Text widget text = Text(root, width = 40, height = 10) text.pack() -------------------------------------------- #Text wigdet with entry text = Text(root, width = 40, height = 10) text.pack() text.config(wrap = 'word') text.insert('1.0 + 2 lines', 'Inserted message') text.insert('1.0 + 2 lines lineend', ' and\nmore and\nmore.') #text.delete('1.0') #text.delete('1.0', '1.0 lineend') #text.delete('1.0', '3.0 lineend + 1 chars') #text.replace('1.0', '1.0 lineend', 'This is the first line.') -------------------------------------------- #Text widget #Add image and button into text image = PhotoImage(file = 'python_logo.gif').subsample(5, 5) # Change path as needed text.image_create('insert', image = image) text.image_create('insert', image = image) button = Button(text, text = 'Click Me') text.window_create('insert', window = button) -------------------------------------------- #Tree view for selection treeview = ttk.Treeview(root) treeview.pack() treeview.insert('', '0', 'item1', text = 'First Item') #(parent note, positon in list, items id, set the text) treeview.insert('', '1', 'item2', text = 'Second Item') treeview.insert('', 'end', 'item3', text = 'Third Item') #addin images to the treeview #two slashes \\ is required for calling image through a specific location. logo = PhotoImage(file = 'C:\\Users\\Solmaz\\Desktop\\Python\\Ex_Files_Python_Tkinter\\Exercise Files\\Ch05\\python_logo.gif').subsample(10,10) #set the image under a sub selection treeview.insert('item2', 'end', 'python', text = 'Python', image = logo) -------------------------------------------- #Treeview, adjust version, adjust configure treeview = ttk.Treeview(root) #generate tree view object treeview.pack() treeview.insert('', '0', 1, text = 'First Item') #(parent note, positon in list, items id, set the text) treeview.insert('', '1', 'item2', text = 'Second Item') treeview.insert('', 'end', 'item3', text = 'Third Item') #addin images to the treeview #two slashes \\ is required for calling image through a specific location. logo = PhotoImage(file = 'C:\\Users\\Solmaz\\Desktop\\Python\\Ex_Files_Python_Tkinter\\Exercise Files\\Ch05\\python_logo.gif').subsample(10,10) #set the image under a sub selection treeview.insert('item2', 'end', 'python', text = 'Python', image = logo) #create headline through document treeview.configure(column = ('version')) treeview.column('version', width = 50, anchor = 'center') treeview.column('version', width = 50, anchor = 'center') treeview.column('#0', width = 150) treeview.heading('version', text = 'Version') treeview.set('python', 'version', '3.4') treeview.item('python', tags = ('software')) treeview.tag_configure('software', background = 'yellow') def callback(event): print(treeview.selection()) treeview.bind('<>', callback) -------------------------------------------- #Menu bar, File command root = Tk() root.option_add('*tearOff', False) menubar = Menu(root) root.config(menu = menubar) file = Menu(menubar) edit = Menu(menubar) help_ = Menu(menubar) #creating menu and menu bar menubar.add_cascade(menu = file, label = 'File') menubar.add_cascade(menu = edit, label = 'Edit') menubar.add_cascade(menu = help_, label = 'Help') #adding comment under menu bar and print it. file.add_command(label = 'New', command = lambda: print('New File')) file.add_separator() file.add_command(label = 'Open...', command = lambda: print('Opening File...')) file.add_command(label = 'Save', command = lambda: print('Saving File...')) #adding logo and photo file.entryconfig('New', accelerator = 'Ctrl+N') logo = PhotoImage(file = 'C:\\Users\\Solmaz\\Desktop\\Python\\Ex_Files_Python_Tkinter\\Exercise Files\\Ch05\\python_logo.gif').subsample(10, 10) file.entryconfig('Open...', image = logo, compound = 'left') file.entryconfig('Open...', state = 'disabled') #make disable of selection file.delete('Save') #can delete command save = Menu(file) #can create delete command file.add_cascade(menu = save, label = 'Save') save.add_command(label = 'Save As',command = lambda: print('Saving As...')) save.add_command(label = 'Save All', command = lambda: print('Saving All...')) #subcommands and print according to selection. #adding choice button choice = IntVar() edit.add_radiobutton(label = 'One', variable = choice, value = 1) edit.add_radiobutton(label = 'Two', variable = choice, value = 2) edit.add_radiobutton(label = 'Three', variable = choice, value = 3) file.post(400, 300) root.mainloop() -------------------------------------------- #Drawing a basic line on the Canvas #Creating geometric figures #Drawing complex shapes on the Canvas from tkinter import * from tkinter import ttk root = Tk() #creating canvas model canvas = Canvas(root) canvas.pack() canvas.config(width = 640, height = 640) #creating a line within coordinates line = canvas.create_line(160, 360, 480, 120, fill = 'blue', width = 5) canvas.itemconfigure(line, fill = 'green') print(canvas.coords(line)) #to show coordinates which is defined as below canvas.coords(line, 0, 0, 320, 240, 640, 0) #creating smooth or circular shapes canvas.itemconfigure(line, smooth = True) canvas.itemconfigure(line, splinesteps = 5) canvas.itemconfigure(line, splinesteps = 100) canvas.delete(line) # to delete all lines created #create shapes and config them rect = canvas.create_rectangle(160, 120, 480, 360) canvas.itemconfigure(rect, fill = 'yellow') oval = canvas.create_oval(160, 120, 480, 360) arc = canvas.create_arc(160, 1, 480, 240) canvas.itemconfigure(arc, start = 0, extent = 180, fill = 'green') poly = canvas.create_polygon(160, 360, 320, 480, 480, 360, fill = 'blue') text = canvas.create_text(320, 240, text = 'Python', font = ('Courier', 32, 'bold')) logo = PhotoImage(file ='C:\\Users\\Solmaz\\Desktop\\Python\\Ex_Files_Python_Tkinter\ \Exercise Files\\Ch05\\python_logo.gif') # Change path as needed image = canvas.create_image(320, 240, image = logo) #adjusting location of logo and text canvas.lift(text) canvas.lower(image) canvas.lower(image, text) #create button as a tk widget button = Button(canvas, text = 'Click Me') canvas.create_window(320, 60, window = button) #config of items canvas.itemconfigure(rect, tags = ('shape')) canvas.itemconfigure(oval, tags = ('shape')) canvas.itemconfigure('shape', fill = 'grey') print(canvas.gettags(oval)) root.mainloop() -------------------------------------------- #Attaching scroll bars to widgets #!/usr/bin/python3 # scrollbar.py by Barron Stone # This is an exercise file from Python GUI Development with Tkinter on lynda.com from tkinter import * from tkinter import ttk root = Tk() canvas = Canvas(root, scrollregion = (0, 0, 640, 480), bg = 'white') xscroll = ttk.Scrollbar(root, orient = HORIZONTAL, command = canvas.xview) yscroll = ttk.Scrollbar(root, orient = VERTICAL, command = canvas.yview) canvas.config(xscrollcommand = xscroll.set, yscrollcommand = yscroll.set) canvas.grid(row = 1, column = 0) xscroll.grid(row = 2, column = 0, sticky = 'ew') yscroll.grid(row = 1, column = 1, sticky = 'ns') root.mainloop() -------------------------------------------- #Canvas #Generating command witn bind mouse or keybord. from tkinter import * from tkinter import ttk root = Tk() #for x and y directions. canvas = Canvas(root, scrollregion = (0, 0, 640, 480), bg = 'white') xscroll = ttk.Scrollbar(root, orient = HORIZONTAL, command = canvas.xview) yscroll = ttk.Scrollbar(root, orient = VERTICAL, command = canvas.yview) canvas.config(xscrollcommand = xscroll.set, yscrollcommand = yscroll.set) canvas.grid(row = 1, column = 0) xscroll.grid(row = 2, column = 0, sticky = 'ew') yscroll.grid(row = 1, column = 1, sticky = 'ns') def canvas_click(event): x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) canvas.create_oval((x-5, y-5, x+5, y+5), fill = 'green') def canvas_click2(event): x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) canvas.create_oval((x-5, y-5, x+5, y+5), fill = 'red') def canvas_click3(event): x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) canvas.create_rectangle((x-5, y-5, x+5, y+5), fill = 'blue') #create a bind between canvas and movement of mouse click #canvas.bind(command number on hardware, function called) canvas.bind('<1>', canvas_click) canvas.bind('<2>', canvas_click2) canvas.bind('<3>', canvas_click3) root.mainloop() -------------------------------------------- #Configuring widget styles #Config button styles, colur and shape from tkinter import * from tkinter import ttk root = Tk() #generating standard buttons button1 = ttk.Button(root, text = 'Button 1') button2 = ttk.Button(root, text = 'Button 2') button1.pack() button2.pack() #active style object to config widget style = ttk.Style() #definition of style themes #several style that can be defined with style.theme_use object. print(style.theme_names()) print(style.theme_use()) style.theme_use('classic') style.theme_use('vista') #config button height, wide, color, shape print(button1.winfo_class()) style.configure('TButton', foreground = 'blue') style.configure('Alarm.TButton', foreground = 'orange', font = ('Arial', 24, 'bold')) button2.configure(style = 'Alarm.TButton') style.map('Alarm.TButton', foreground = [('pressed', 'black'), ('disabled', 'black')]) #style map changes color regarding press or disable color #giving information about config, overall. print(style.layout('TButton')) print(style.element_options('Button.label')) print(style.lookup('TButton', 'foreground')) root.mainloop() -------------------------------------------- #Prompting users with the Messagebox and dialogs #Message box from tkinter import messagebox #create message boxes and pop up windows. #Create box and ask yes or no. messagebox.showinfo(title = "A Friendly Message", message = 'Hello, Tkinter!') print(messagebox.askyesno(title = 'Hungry?', message = 'Do you want SPAM?')) #browse a file with filedialog from tkinter import filedialog filename = filedialog.askopenfile() print(filename.name) #choose a color and print color id from tkinter import colorchooser print(colorchooser.askcolor(initialcolor = "#FFFFFF")) -------------------------------------------- ------------------------------------------- GEOMETRY MANAGEMENT (Pack, Grid, Place) --------------------------------------------------------------------------- #Pack geometry manager #Pros: simplest to use, multiple operation #Cons: Limited for complex layouts from tkinter import * from tkinter import ttk root = Tk() #anchor definition could stable location nw=northwest or sw=southwest ttk.Label(root, text = 'Hello, Tkinter!', background = 'yellow').pack(side = LEFT, anchor = 'sw') #padx and pady also specify directions. ttk.Label(root, text = 'Hello, Tkinter!', background = 'blue').pack(side = LEFT, padx = 10, pady = 10) label = ttk.Label(root, text = 'Hello, Tkinter!', background = 'green') label.pack(side = LEFT, ipadx = 10, ipady = 10) print(label) #for the purpose of extend widgets for widget in root.pack_slaves(): widget.pack_configure(fill = BOTH, expand = True) print(widget.pack_info()) #instead of delete, we can ignore widget within forget() object. label.pack_forget() root.mainloop() --------------------------------------------------------------------------- #Grid geometry manager #Pros: easy to organize, modern GUI layouts #Cons: Slightly complex than pack geo mangr. from tkinter import * from tkinter import ttk root = Tk() #every widget has own grid cell between 0 0 to 1 1 #sticky is used to widget resize and relocation #pad expands gray scale through inner cell of widgets ttk.Label(root, text = 'Yellow', background = 'yellow').grid(row = 0, column = 2, rowspan = 2, sticky = 'nsew') ttk.Label(root, text = 'Blue', background = 'Blue').grid(row = 1, column = 0, columnspan = 2, sticky = 'nsew') ttk.Label(root, text = 'Green', background = 'Green').grid(row = 0, column = 0, sticky = 'nsew', padx = 10, pady = 10) ttk.Label(root, text = 'Orange', background = 'Orange').grid(row = 0, column = 1, sticky = 'nsew', ipadx = 25, ipady = 25) #generates within min area widget. #ttk.Label(root, text = 'Yellow', background = 'yellow').grid(row = 0, column = 2) #ttk.Label(root, text = 'Blue', background = 'Blue').grid(row = 1, column = 0) #ttk.Label(root, text = 'Green', background = 'Green').grid(row = 0, column = 0) #ttk.Label(root, text = 'Orange', background = 'Orange').grid(row = 0, column = 1) #adjust expanding process through widgets individually root.rowconfigure(0, weight = 1) root.rowconfigure(1, weight = 3) root.columnconfigure(2, weight = 1) #also available as in pack grid_slaves() grid_configure() grid_info() grid_forget() root.mainloop() --------------------------------------------------------------------------- #Place geometry manager. #Pros: exact location control. #Cons: control a large amount of widgets. #!/usr/bin/python3 # place.py by Barron Stone # This is an exercise file from Python GUI Development with Tkinter on lynda.com from tkinter import * from tkinter import ttk root = Tk() root.geometry('640x480+200+200') #relx and rely relative locations for widgets. ttk.Label(root, text = 'Yellow', background = 'yellow').place(x = 100, y = 50, width = 100, height = 50) ttk.Label(root, text = 'Blue', background = 'blue').place(relx = 0.5, rely = 0.5, anchor = 'center', relwidth = 0.5, relheight = 0.5) ttk.Label(root, text = 'Green', background = 'green').place(relx = 0.5, x = 100, rely = 0.5, y = 50) ttk.Label(root, text = 'Orange', background = 'orange').place(relx = 1.0, x = -5, y = 5, anchor = 'ne') root.mainloop() #also available as in pack place_slaves() place_configure() place_info() place_forget() -------------------------------------------- EVENT HANDLING -------------------------------------------- #callback #Calling command with button #is used to get data from hardware and select a specific command from tkinter import * from tkinter import ttk def callback(number): print(number) #buttons have to be out of function to get data after function goes through. #On the contrary, commands can't be get in function. root = Tk() ttk.Button(root, text = '1', command = lambda: callback(1)).pack() ttk.Button(root, text = '2', command = lambda: callback(2)).pack() ttk.Button(root, text = '3', command = lambda: callback(3)).pack() root.mainloop() -------------------------------------------- #binding to keyboard events from tkinter import * from tkinter import ttk def key_press(event): print('type: {}'.format(event.type)) print('widget: {}'.format(event.widget)) print('char: {}'.format(event.char)) print('keysym: {}'.format(event.keysym)) print('keycode: {}\n'.format(event.keycode)) def shortcut(action): print(action) root = Tk() root.bind('', lambda e: shortcut('Copy')) root.bind('', lambda e: shortcut('Paste')) root.mainloop() -------------------------------------------- #Binding mouse events # , , <1> represent; 1 was pressed # ; 1 was released. # or , button 1 was double. #