分享一個(gè)Python Tkinter基礎(chǔ)控件用法的入門例子,包括窗口的顯示、顯示內(nèi)置圖片、彈出窗口、菜單等.
例子,Python Tkinter基礎(chǔ)控件的用法
# -*- coding: utf-8 -*-from Tkinter import *def btn_click():b2['text'] = 'clicked'evalue = e.get()PRint 'btn Click and Entry value is %s' % evaluedef btn_click_bind(event):print 'enter b2'def show_toplevel():top = Toplevel()top.title('2號(hào)窗口')Label(top, text='這是2號(hào)窗口').pack()root = Tk()root.title('1號(hào)窗口')# 顯示內(nèi)置圖片# x = Label(root, bitmap='warning')l = Label(root, fg='red', bg='blue',text='wangwei', width=34, height=10)l.pack()# command 指定按鈕調(diào)用的函數(shù)b = Button(root, text='clickme', command=btn_click)b['width'] = 10b['height'] = 2b.pack()# 使用bind 方式關(guān)聯(lián)按鈕和函數(shù)b2 = Button(root, text = 'clickme2')b2.configure(width = 10, height = 2, state = 'disabled')b2.bind("<Enter>", btn_click_bind)b2.pack()# 彈出Toplevel窗口b3 = Button(root, text = 'showToplevel', command=show_toplevel)b3.pack()# 輸入框e = Entry(root, text = 'input your name')e.pack()# 密碼框epwd = Entry(root, text = 'input your pwd', show = '*')epwd.pack()# 菜單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)# 彈出菜單def pop(event):submenu.post(event.x_root, event.y_root)# www.jbxue.com# 獲取鼠標(biāo)左鍵點(diǎn)擊的坐標(biāo)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('<Button-3>', pop)root.bind('<Button-1>', get_clickpoint)root.mainloop()
效果圖:

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注