`
无拘的驰骋
  • 浏览: 45249 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

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

阅读更多
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()
分享到:
评论

相关推荐

    python-钩子函数-通俗.docx

    python 钩子函数 通俗 Python钩子函数是一种非常有用的编程技术,它可以让程序员在特定的事件发生时执行特定的代码。这种技术在很多应用程序中都得到了广泛的应用,比如GUI应用程序、Web应用程序等等。 Python钩子...

    Python程序设计案例教程-第-4-章-图形用户界面设计.pptx

    4.1.1常用设计图形界面的模块 Python有多种用于设计图形用户界面的模块,常用的模块有如下几种: tkinter 使用Tk平台,Python系统自带的标准图形用户界面库。 wxpython 基于wxWindows,具有跨平台的特性。 Python...

    python基础教程:在python tkinter界面中添加按钮的实例

    tkinter是python自带的GUI库,可以实现简单的GUI交互,该例子添加了五种不同效果的Button,如图: from tkinter import * from tkinter import messagebox #python3.0的messagebox,属于tkinter的一个组件 top = Tk...

    Python GUI编程学习笔记之tkinter事件绑定操作详解

    本文实例讲述了Python GUI编程学习笔记之tkinter事件绑定操作。分享给大家供大家参考,具体如下: 相关内容: command bind protocol 首发时间:2018-03-04 19:26 command: command是控件中的一个参数,如果...

    guitk:适用于Tk的Python GUI工具包(guitk)

    适用于TK的Python GUI工具包(GUITk) 概要 GUITk是设计一个轻量级的框架,它简化了创建简单的GUI实验。 程式码范例 """Simple Hello World example using guitk """ import guitk class HelloWindow ( guitk . ...

    cl-simple-tk:Lisp中TK gui库的简单包装

    受python tkinter软件包的启发,这是一个非常简单的tcl/tk包装器,用于普通lisp。 例子 一个带有一个按钮的窗口的小示例。 (with-tk-root (r) ( setf (window-title r) " Hi! " ) (pack (button :parent r :...

    Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解

    本文实例讲述了Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法。分享给大家供大家参考,具体如下: 相关内容: tkinter的使用 1.模块的导入 2.使用 3.控件介绍 Tk Button Label Frame Toplevel ...

    对python中GUI,Label和Button的实例详解

    如下所示: #coding=utf-8 import Tkinter top=Tkinter.Tk() #400x300:代表初始化时主窗口的大小,300,100分别代表窗口的初始化位置 ...button=Tkinter.Button(top,text='quit',command=top.quit,bg='red',fg='o

    Python中使用Tkinter模块创建GUI程序实例

    使用Tkinter模块来创建简单的GUI程序。 Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel等。 例: ...

    python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例

    注意主窗口一定要为tk.Tk(),在主窗口上通过button的点击相应子函数创建子窗口,注意此时创建出来的窗口必须是Toplevel,否则出错。 至于用图片在窗口上绘图,则按代码所示即可。 # -*- coding: utf-8 -*- """ ...

    cryptocoin-market-making

    Python GUI Python有多个GUI框架。 TKinter是其中之一。 TKinter的示例GUI: from Tkinter import Tk, BOTH from ttk import Frame, Button, Style class Example(Frame): def __init__(self, parent): Frame._...

    python tkinter之 复选、文本、下拉的实现

    win.title("Python GUI") # 添加标题 ttk.Label(win, text="Chooes a number").grid(column=1, row=0) # 添加一个标签0 ttk.Label(win, text="Enter a name:").grid(column=0, row=0) # 设置其在界面中出现的位置 # ...

    使用Tkinter进行GUI程序设计笔记(一)

    Tkinter模块包含了许多创建GUI的类,实现图形用户界面的创建,可以用来作为python面向对象编程设计的入门。 文章目录 创建Window窗口 Tkinter组件介绍 Label标签组件 Button按钮组件 Entry单行文本 Listbox列表框...

Global site tag (gtag.js) - Google Analytics