Python Tkinter基础控件用法

yipeiwu_com6年前Python基础

本文实例展示了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'] = 10
b['height'] = 2
b.pack()
# 使用bind 方式关联按钮和函数
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)

# 获取鼠标左键点击的坐标
def get_clickpoint(event):
  print event.x, event.y

# frame
for x in ['red', 'blue', 'yellow']:
  Frame(height = 20, width = 20, bg = x).pack()

root['menu'] = xmenu
root.bind('<Button-3>', pop)
root.bind('<Button-1>', get_clickpoint)
root.mainloop()

运行效果如下图所示:

希望本文所述对大家的Python程序设计有所帮助。

相关文章

使用Py2Exe for Python3创建自己的exe程序示例

使用Py2Exe for Python3创建自己的exe程序示例

最近使用Python 3.5写了一个GUI小程序,于是想将该写好的程序发布成一个exe文件,供自己单独使用。至于通过安装的方式使用该程序,我没有探索,感兴趣的读者可以自己摸索。 1 介绍...

Django admin model 汉化显示文字的实现方法

1、将添加blog的后台基本操作 在blog文件夹下新建一个admin.py文件加入一下代码: from django.contrib import admin from djcm...

Python判断一个list中是否包含另一个list全部元素的方法分析

本文实例讲述了Python判断一个list中是否包含另一个list全部元素的方法。分享给大家供大家参考,具体如下: 你可以用for in循环+in来判断 #!/usr/bin/env...

基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)

1、CSV (1)写csv文件 import csv def writecsv(path,data): with open(path, "w") as f: wri...

python中stdout输出不缓存的设置方法

考虑以下python程序:复制代码 代码如下:#!/usr/bin/env pythonimport syssys.stdout.write("stdout1 ")sys.stderr....