Tkinter 4 Button

References

Button插件就是用来触发某一功能或者事件的。

Button 控件类

属性

方法

例子1: 颜色,高宽,阴影,图文混合,边框,回调

    def _gui_int(self):
        # 1. set call back func
        tier_1 = tk.Frame(self.window, height=100)
        tier_2 = tk.Frame(self.window, height=100)
        tier_3 = tk.Frame(self.window, height=100)
        tier_1.pack(side='top')
        tier_2.pack(side='top')
        tier_3.pack(side='top')
        # different button relief
        # relief: flat, groove, raised, ridge, solid, or sunken
        # bitmap: error, info, question, warning, questhead, hourglass, gray12, gray25, gray50, gray75
        # compound: left, right, top, bottom, center
        self.btn_1 = tk.Button(tier_1,
                               text="say hi",
                               command=lambda: self.print_with("Hello, "),
                               bg="#080808",
                               fg='red',
                               padx=10,
                               pady=5,
                               bitmap='info',
                               compound='left',
                               relief=tk.FLAT)
        self.btn_1.pack(side=tk.LEFT)

        # set width, height
        # justify: left, right, top, bottom
        # wraplength: set length to auto-wrap
        # borderwidth(bd): by default, 1/2 pixel
        # anchor: 'n','s','e','w','ne','nw','se','sw',center
        self.btn_2 = tk.Button(tier_1,
                               text='say \nbye',
                               width=10,
                               height=3,
                               justify='left',
                               wraplength=10,
                               bd=10,
                               anchor='s',
                               command=lambda: self.print_with("Bye, ")
                               )
        self.btn_2.pack(side=tk.RIGHT)

        self.label = tk.Label(tier_2,
                              text="How are you doing?",
                              bitmap='questhead',
                              width=200,
                              height=50,
                              compound='left',)
        self.label.pack()
        self.output = tk.Text(tier_3)
        self.output.pack()

例子2:状态,绑定变量,焦点问题

		# state: normal, active, disabled
        # tk.StringVar() to set the textvariable
        # bind()
        tier_4 = tk.Frame(self.window, height=20)
        tier_4.pack(side='top')

        self.btn_3 = tk.Button(tier_4, text='like', command=lambda: self.print_with("I like "), state='normal')
        self.btn_3.pack(side=tk.LEFT)
        self.btn_4 = tk.Button(tier_4, text='hate', command=lambda: self.print_with("I hate "), state='disabled')
        self.btn_4.pack(side=tk.LEFT)
        self.btn_var = tk.StringVar()
        self.btn_5 = tk.Button(tier_4, textvariable=self.btn_var)
        self.btn_var.set('~Like~')
        # self.btn_5.bind("<Enter>", self.change_text)
        self.btn_5.bind("<Return>", self.change_text)
        self.btn_5.pack(side=tk.LEFT)

    def change_text(self, event):
        # if self.btn_var.get() == 'click':
        if self.btn_5['text'] == '~Like~':
            self.btn_var.set('~Hate~')
        else:
            self.btn_var.set('~Like~')

    def print_with(self, _str):
        # print(str(_str), "world!")
        self.output.delete(0.0, tk.END)
        self.output.insert(0.0, str(_str) + " world")

使用Lambda让回调函数带参数

一般情况下,事件处理都是下面这样的

#coding=utf-8  
  
import Tkinter  
  
def handler():  
    '''''事件处理函数'''  
    print "handler"  
  
  
if __name__=='__main__':  
    root = Tkinter.Tk()  
    # 通过中介函数handlerAdapotor进行command设置  
    btn = Tkinter.Button(text=u'按钮', command=handler)  
    btn.pack()  
    root.mainloop()  

handler()函数需要参数该怎么办呢,很简单,使用lambda

import Tkinter  
  
def handler(a, b, c):  
    '''''事件处理函数'''  
    print "handler", a, b, c  
  
  
if __name__=='__main__':  
    root = Tkinter.Tk()  
    # 通过中介函数handlerAdapotor进行command设置  
    btn = Tkinter.Button(text=u'按钮', command=lambda : handler(a=1, b=2, c=3))  
    btn.pack()  
    root.mainloop()  

使用bind怎么传参数呢

对于想使用event的情况,像btn.bind(“", handler),又该怎么办呢,好说再写个中间适配器函数

import Tkinter  
  
def handler(event, a, b, c):  
    '''''事件处理函数'''  
    print event  
    print "handler", a, b, c  
  
def handlerAdaptor(fun, **kwds):  
    '''''事件处理函数的适配器,相当于中介,那个event是从那里来的呢,我也纳闷,这也许就是python的伟大之处吧'''  
    return lambda event,fun=fun,kwds=kwds: fun(event, **kwds)  
  
if __name__=='__main__':  
    root = Tkinter.Tk()  
    btn = Tkinter.Button(text=u'按钮')  
  
    # 通过中介函数handlerAdaptor进行事件绑定  
    btn.bind("<Button-1>", handlerAdaptor(handler, a=1, b=2, c=3))  
  
    btn.pack()  
    root.mainloop()  

如果不想使用a=1,b=2,c=3字典式的参数,可以适当的修改handlerAdaptor()的*kwds参数,换成args,灵活得很!*、的意思,我想学python的人都应该知道吧!

Table of Contents