python基础知识-GUI编程-TK-Button

[color=blue][b]1、最基础的用法[/b][/color]

其实和其他的TK组件的用法基本相同,建下面的代码行

root = Tkinter.TK()

ttk.Button(root, text="submit").grid()

root.mainloop()

其中的text属性就是button的名称

[color=blue][b]2、绑定某个函数以进行数据 处理
[/b][/color]
def testClick():
print "this is the testClick method"

root = Tkinter.TK()

ttk.Button(root, text = "submit", [color=red]command = testClick[/color]).grid()

root.mainloop()

[b][color=blue]3、通过绑定事件来进行数据处理[/color][/b]


def testClick():
print "this is the testClick method"

def testEvent(event):
print "this is the testEvent method"
print "the time of event is ", event.time

root = Tkinter.Tk()


b = ttk.Button(root, text="submit", command = testClick)
[color=red]b.bind("<Return>", testEvent)[/color]
b.grid()

root.mainloop()

和之前不一样的地方是,需要明确一个对象名称,以方便调用bind函数;另外,还有一个需要注意的地方是,bind函数的调用是在合入frame之前,也就是调用grid或者pack函数之前

[b][color=blue]4、点击Button处理数据成功后,弹出对话框
[/color][/b]
弹出对话框的处理是使用tkMessageBox函数,代码如下:

import tkMessageBox


def testClick():
print "this is the testClick method"
[color=red]tkMessageBox.showeinfo(message = 'this method is success')[/color]


def testEvent(event):
print "this is the testEvent method"
print "the time of event is ", event.time

root = Tkinter.Tk()
root.geometry('400x150+400+200')
root.title("test")

b = ttk.Button(root, text="submit", command = testClick)
b.bind("<Return>", testEvent)
b.grid()

root.mainloop()

这个tkMessageBox.showinfo函数显示了一个提示框,有一个确定按钮


具体参考:[url]http://www.tkdocs.com/tutorial/windows.html[/url]

[color=blue][b]5、较为复杂的对话框[/b][/color]

很多场景下需要使用较为复杂的对话框,例如:有一个确定和取消按钮。针对这些场景,tkMessageBox提供了如下的方法:
askokcancel, askyesno, askyesnocancel, askretrycancel, askabortretryignore

这些方法的特点是,当选择ok或者是yes的时候,返回值为true;这样就可以根据返回值做相应的处理动作。事例代码如下:


import tkMessageBox

def testClick():
print "this is the testClick method"
[color=red]flag = tkMessageBox.askokcancel(message = "this is okcancel")

if flag == True:
root.destroy()
elif flag == False:
print "cancel"[/color]

def testEvent(event):
print "this is the testEvent method"
print "the time of event is ", event.time

root = Tkinter.Tk()
root.geometry('400x150+400+200')
root.title("test")

b = ttk.Button(root, text="submit", command = testClick)
b.bind("<Return>", testEvent)
b.grid()

root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值