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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值