Python Tkinter Basic Control Learning
#-*-Coding: UTF-8-*-from Tkinter import * def btn_click (): b2 ['text'] = 'clicked' evalue = e. get () print 'btn Click and Entry value is % s' % evalue def btn_click_bind (event): print 'enter b2' def show_toplevel (): top = Toplevel () top. title ('window 2 ') Label (top, text =' This is window 2 '). pack () root = Tk () root. title ('window 1 ') # display built-in images # x = Label (root, bitmap = 'warning') l = Label (root, fg = 'red ', bg = 'blue', text = 'hangwei', width = 34, height = 10) l. pack () # command specifies the function B = Button (root, text = 'clickme ', command = btn_click) called by the Button) B ['width'] = 10b ['height'] = 2b. pack () # bind the join Button and function b2 = Button (root, text = 'clickme2') b2.configure (width = 10, height = 2, state = 'Disabled ') b2.bind ("
", Btn_click_bind) b2.pack () # The Toplevel window b3 = Button (root, text = 'showtoplevel', command = show_toplevel) b3.pack () # input box e = Entry (root, text = 'input your name') e. pack () # Password box epwd = Entry (root, text = 'input your pwd', show = '*') epwd. pack () # menu def menu_click (): print 'I am Menu' xmenu = Menu (root) submenu = menu (xmenu, tearoff = 0) for item in ['java ', 'cpp ', 'C', 'php']: xmenu. add_command (label = item, command = menu_click) for item in ['think in Java', 'java web', 'android']: submenu. add_command (label = item, command = menu_click) xmenu. add_cascade (label = 'progame', menu = submenu) # pop-up menu def pop (event): submenu. post (event. x_root, event. y_root) # obtain the coordinate def get_clickpoint (event): print event. x, event. y # framefor x in ['red', 'blue', 'yellow']: Frame (height = 20, width = 20, bg = x ). pack () root ['menu '] = xmenuroot. bind ('
', Pop) root. bind ('
', Get_clickpoint) root. mainloop ()