设为首页 收藏本站
查看: 597|回复: 0

[经验分享] python基础知识-GUI编程-TK-Button

[复制链接]

尚未签到

发表于 2017-5-4 12:50:31 | 显示全部楼层 |阅读模式
1、最基础的用法

        其实和其他的TK组件的用法基本相同,建下面的代码行
      
root = Tkinter.TK()

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

root.mainloop()

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

2、绑定某个函数以进行数据 处理

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

root = Tkinter.TK()

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

root.mainloop()

3、通过绑定事件来进行数据处理


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)
b.bind("<Return>", testEvent)
b.grid()

root.mainloop()

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

4、点击Button处理数据成功后,弹出对话框

        弹出对话框的处理是使用tkMessageBox函数,代码如下:

import tkMessageBox


def testClick():
print "this is the testClick method"
        tkMessageBox.showeinfo(message = 'this method is success')


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函数显示了一个提示框,有一个确定按钮




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

5、较为复杂的对话框

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

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


import tkMessageBox

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

if flag == True:
  root.destroy()
elif flag == False:
  print "cancel"


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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373056-1-1.html 上篇帖子: [python]通过ElementTree来操作XML 下篇帖子: Python实现快速排序算法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表