本文实例展示了python tkinter基础控件的用法,分享给大家供大家参考之用。具体方法如下:
# -*- 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('2号窗口') label(top, text='这是2号窗口').pack()root = tk()root.title('1号窗口')# 显示内置图片# x = label(root, bitmap='warning')l = label(root, fg='red', bg='blue',text='wangwei', width=34, height=10)l.pack()# command 指定按钮调用的函数b = button(root, text='clickme', command=btn_click)b['width'] = 10b['height'] = 2b.pack()# 使用bind 方式关联按钮和函数b2 = button(root, text = 'clickme2')b2.configure(width = 10, height = 2, state = 'disabled')b2.bind(, 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)# 获取鼠标左键点击的坐标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()
运行效果如下图所示:
希望本文所述对大家的python程序设计有所帮助。