Python GUI with Tkinter-1-Introduction
The link above is the first link to a Python tkinter video series. Although English is not good, but, the program still can understand (according to do), so can not find the Chinese video to see these English video is also possible.
Here are the exercises I'm looking at in the video, which can be run under python2.7.
001:hello,world:
From Tkinter import Label, tkroot = Tk () Thelabel = Label (Root, text= "This is too Easy") Thelabel.pack () Root.mainloop ()
002:button Pack Layout
From Tkinter Import *root = Tk () Topframe = FRAME (root) topframe.pack () BottomFrame = FRAME (root) bottomframe.pack (side= BOTTOM) button1 = button (Topframe, text= "button 1", fg= "red") Button2 = button (topframe, text= "button 2", fg= "Blue") Button3 = button (topframe, text= "button 3", fg= "green") Button4 = button (bottomframe, text= "button 4", fg= "purple") Button1.pack (Side=left) button2.pack (side=left) button3.pack (side=left) button4.pack (Side=BOTTOM) Root.mainloop ()
003:label and pack layouts
From Tkinter Import *root = Tk () one = label (root, text= "one", bg= "Red", fg= "white"), and the label (Root, text= "one", bg= "green") ", fg=" BLACK ") three = Label (root, text=" three ", bg=" Blue ", fg=" white ") One.pack () two.pack (fill=x) three.pack (side=left , fill=y) Root.mainloop ()
004:grid layout.
From Tkinter Import *root = Tk () Label_1 = label (root, text= "Name") label_2 = label (root, text= "Password") Entry_1 = Entry (ro OT) Entry_2 = entry (root) Label_1.grid (row=0) Label_2.grid (row=1) Entry_1.grid (row=0, Column=1) Entry_2.grid (Row=1, column=1) Root.mainloop ()
005:grid layout
From Tkinter Import *root = Tk () Label_1 = label (root, text= "Name") label_2 = label (root, text= "Password") Entry_1 = Entry (ro OT) Entry_2 = entry (root) Label_1.grid (row=0, sticky=e) Label_2.grid (row=1, sticky=e) Entry_1.grid (row=0, Column=1) Entry_2.grid (Row=1, column=1) c = Checkbutton (Root, text= "Keep me Logged in") C.grid (columnspan=2) Root.mainloop ()
006:button and Events
From Tkinter Import *root = Tk () def printname (): print ("Chello My name is bucky!") Button_1 = button (root, text= "Print My Name", Command=printname) Button_1.pack () Root.mainloop ()
007: Bind event: Left key, Middle key, right button
#coding: utf8from Tkinter Import *root = Tk () def printname (event): print ("Chello My name is bucky!") Button_1 = button (root, text= "Print My Name") ' <Button-1> left mouse button <Button-2> middle mouse button <Button-3> right mouse button ' Button_1.bind ("<Button-1>", Printname) Button_1.pack () Root.mainloop ()
008: Bind event: Left key, Middle key, right button
From Tkinter Import *root = Tk () def leftclick (event): print "left" def Middleclick (event): print "Middle" C3/>def RightClick (Event): print "right" frame = frame (root, width=300, height=250) frame.bind ("<Button-1>", Leftclick) Frame.bind ("<Button-2>", Middleclick) frame.bind ("<Button-3>", RightClick) Frame.pack () Root.mainloop ()
009:python GUI with tkinter-8-using Classes
#-*-Coding:utf-8-*-"Python GUI with tkinter-8-using Classes" from Tkinter import *class buckysbuttons: def __ini T__ (self, Master): frame = FRAME (master) Frame.pack () Self.printbutton = button (frame, text= "Print Message ", Command=self.printmessage) self.printButton.pack (side=left) Self.quitbutton = Button (frame, text = "Quit", Command=frame.quit) self.quitButton.pack (side=left) def printmessage (self): print "Wow, this Actually worked! " root = Tk () b = buckysbuttons (root) Root.mainloop ()
010:python GUI with tkinter-9-creating Drop Menus
#-*-Coding:utf-8-*-"Python GUI with tkinter-9-creating Drop Menus" from Tkinter import *def doNothing (): PR int ("OK I won ' t ...") root = Tk () menu = menu (Root) root.config (menu=menu) submenu = Menu (menu) menu.add_cascade (label= " File ", Menu=submenu) Submenu.add_command (label=" New Project ... ", command=donothing) Submenu.add_command (label=" New ... ", command=donothing) Submenu.add_separator () Submenu.add_command (label=" Exit ", command=donothing) EditMenu = Menu (menu) Menu.add_cascade (label= "Edit", Menu=editmenu) Editmenu.add_command (label= "Redo", command=donothing) Root.mainloop ()
011:python GUI with Tkinter-10-creating a Toolbar
#-*-Coding:utf-8-*-"Python GUI with tkinter-10-creating a Toolbar" "from Tkinter import *def doNothing (): print (" Ok ok I won ' t ... ") root = Tk () # * * * * * * * * * * Main Menu *****menu = menu (Root) root.config (menu=menu) submenu = Menu (menu) Menu.add_ca Scade (label= "File", Menu=submenu) Submenu.add_command (label= "New Project ...", command=donothing) submenu.add_ Command (label= "New ...", command=donothing) Submenu.add_separator () Submenu.add_command (label= "Exit", command= doNothing) Editmenu = Menu (menu) Menu.add_cascade (label= "Edit", Menu=editmenu) Editmenu.add_command (label= "Redo", command=donothing) # * * * * * * * * * * Toolbar *****toolbar = Frame (root, bg= "blue") insertbtn = Button (Toolbar, text= "Insert Image", c ommand=donothing) Insertbtn.pack (Side=left, padx=2, pady=2) printbtn = Button (toolbar, text= "Print", command=donothing ) Printbtn.pack (Side=left, padx=2, pady=2) toolbar.pack (Side=top, Fill=x) Root.mainloop ()
012: python GUI with tkinter-11-adding the Status Bar
#-*-Coding:utf-8-*-"Python GUI with tkinter-11-adding the Status Bar" from Tkinter import *def doNothing (): Print ("OK I won ' t ...") root = Tk () # * * * * * * * * * * * * Main Menu *****menu = menu (Root) root.config (menu=menu) submenu = Menu (menu) menu.add_ Cascade (label= "File", Menu=submenu) Submenu.add_command (label= "New Project ...", command=donothing) submenu.add_ Command (label= "New ...", command=donothing) Submenu.add_separator () Submenu.add_command (label= "Exit", command= doNothing) Editmenu = Menu (menu) Menu.add_cascade (label= "Edit", Menu=editmenu) Editmenu.add_command (label= "Redo", command=donothing) # * * * * * * * * * * Toolbar *****toolbar = Frame (root, bg= "blue") insertbtn = Button (Toolbar, text= "Insert Image", c ommand=donothing) Insertbtn.pack (Side=left, padx=2, pady=2) printbtn = Button (toolbar, text= "Print", command=donothing ) Printbtn.pack (Side=left, padx=2, pady=2) toolbar.pack (Side=top, fill=x) # * * * * * Status Bar *****status = Label (root, text = "Preparing to does nothing ...", bd=1, Relief=sunken, anchor=w) status.pack(Side=bottom, Fill=x) Root.mainloop ()
013:python GUI with Tkinter-12-messagebox
#-*-Coding:utf-8-*-' Python GUI with tkinter-12-messageboxhow to create a message box with Tkinter?http://stackoverflo W.com/questions/1052420/how-to-create-a-message-box-with-tkinter ' from tkinter import *import tkmessageboxroot = Tk () tkmessagebox.showinfo ("Window Title", "Monkeys can live up to years.") msg = Tkmessageboxanswer = Msg.askquestion ("Question1", "Do I like silly faces?") If answer = = ' Yes ': print (' 8===d~ ') Root.mainloop ()
014:python GUI with Tkinter-13-shapes and Graphics
#-*-Coding:utf-8-*-"Python GUI with Tkinter-13-shapes and Graphics" from Tkinter import *root = Tk () canvas = Canvas ( Root, width=200, height=100) canvas.pack () Blackline = canvas.create_line (0, 0, $, 0) RedLine = canvas.create_line Fill= "Red") Greenbox = Canvas.create_rectangle (+, fill= "green") # Canvas.delete (redLine) canvas . Delete (All) Root.mainloop ()
015:python GUI with Tkinter-14-images and Icons
#-*-Coding:utf-8-*-' Python GUI with Tkinter-14-images and Icons (1.1) In this example encountered a problem "_tkinter. TCLERROR:COULDN ' t recognize data in image file ' (1.2) http://stackoverflow.com/questions/27599311/ Tkinter-photoimage-doesnt-not-support-png-image (1.3) http://effbot.org/tkinterbook/photoimage.htm (1.4) Workaround refer to the link above. Introduced "from PIL import image, Imagetk"---------------(2) Chinese catalog to add u "..." "from Tkinter import *from PIL import image, image Tkroot = Tk () imglist = ["Image001.png", "kaola.jpg", U "koala. jpg"]print (imglist[-1]) image = Image.open (imglist[-1]) # photo = Photoimage (file= "image001.png") photo = Imagetk.photoimage (image) label = label (Root, Image=photo) Label.pack () Root.mainloop ()
Python GUI with Tkinter (from YouTube) can find many programming videos on YouTube ...