遇到一个 TKinter 事件绑定的问题

2021-04-06 20:38:23 +08:00
 tars2018

一个计算器程序

-- encoding: utf-8 --

''' @文件 :Caluators.py @说明 : @时间 :2021-04-06 19:43:09 @作者 : @版本 :1.0 '''

import os,sys,time,logging import tkinter from typing import Text logging.basicConfig(level=logging.INFO)

class Caluator(tkinter.Tk): def init(self, master=None): tkinter.Tk.init(self, master) self.caluator_str = [] self.createWidgets()

def createWidgets(self):
    text_area= tkinter.Text(self)
    text_area.config(width=120, height=5, bg="white")
    text_area.pack()

    numbers_frame = tkinter.Frame(self, width=120, height=10)
    numbers_frame.pack(ipadx=30, ipady=10)

    button7 = tkinter.Button(numbers_frame, text="7")
    button7.config(font="-family {黑体} -size 18")
    button7.config(width=3, height=1)
    button7.pack(side="left", padx=10, pady=10)
    button7.bind("<Button-1>", self.caluator_func(7))

    button8 = tkinter.Button(numbers_frame, text="8")
    button8.config(width=3, height=1)
    button8.config(font="-family {黑体} -size 18")
    button8.pack(side="left", padx=10)

    button9 = tkinter.Button(numbers_frame, text="9")
    button9.config(width=3, height=1)
    button9.config(font="-family {黑体} -size 18")
    button9.pack(side="left", padx=10)

    buttonc = tkinter.Button(numbers_frame, text="C")
    buttonc.config(width=3, height=1)
    buttonc.config(font="-family {黑体} -size 18")
    buttonc.pack(side="left", padx=10)

    numbers_frame1 = tkinter.Frame(self, width=120, height=10)
    numbers_frame1.pack(ipadx=20, ipady=10)

    button4 = tkinter.Button(numbers_frame1, text="4")
    button4.config(width=3, height=1)
    button4.config(font="-family {黑体} -size 18")
    button4.pack(side="left", padx=10)

    button5 = tkinter.Button(numbers_frame1, text="5")
    button5.config(width=3, height=1)
    button5.config(font="-family {黑体} -size 18")
    button5.pack(side="left", padx=10)

    button6 = tkinter.Button(numbers_frame1, text="6")
    button6.config(width=3, height=1)
    button6.config(font="-family {黑体} -size 18")
    button6.pack(side="left", padx=10)

    buttondcr = tkinter.Button(numbers_frame1, text="/")
    buttondcr.config(width=3, height=1)
    buttondcr.config(font="-family {黑体} -size 18")
    buttondcr.pack(side="left", padx=10)
    
    numbers_frame2 = tkinter.Frame(self, width=120, height=10)
    numbers_frame2.pack(ipadx=20, ipady=10)

    button1 = tkinter.Button(numbers_frame2, text="1")
    button1.config(width=3, height=1)
    button1.config(font="-family {黑体} -size 18")
    button1.pack(side="left", padx=10)

    button2 = tkinter.Button(numbers_frame2, text="2")
    button2.config(width=3, height=1)
    button2.config(font="-family {黑体} -size 18")
    button2.pack(side="left", padx=10)

    button3 = tkinter.Button(numbers_frame2, text="3")
    button3.config(width=3, height=1)
    button3.config(font="-family {黑体} -size 18")
    button3.pack(side="left", padx=10)

    buttonplus = tkinter.Button(numbers_frame2, text="*")
    buttonplus.config(width=3, height=1)
    buttonplus.config(font="-family {黑体} -size 18")
    buttonplus.pack(side="left", padx=10)

    numbers_frame3 = tkinter.Frame(self, width=120, height=10)
    numbers_frame3.pack(ipadx=20, ipady=10)

    button0 = tkinter.Button(numbers_frame3, text="0")
    button0.config(width=3, height=1)
    button0.config(font="-family {黑体} -size 18")
    button0.pack(side="left", padx=10)

    buttond = tkinter.Button(numbers_frame3, text=".")
    buttond.config(width=3, height=1)
    buttond.config(font="-family {黑体} -size 18")
    buttond.pack(side="left", padx=10)

    buttonr = tkinter.Button(numbers_frame3, text="<-")
    buttonr.config(width=3, height=1)
    buttonr.config(font="-family {黑体} -size 18")
    buttonr.pack(side="left", padx=10)

    buttone = tkinter.Button(numbers_frame3, text="=")
    buttone.config(width=3, height=1)
    buttone.config(font="-family {黑体} -size 18")
    buttone.pack(side="left", padx=10)

def caluator_func(self, numbers):
    self.caluator_str.append(numbers)
    print(numbers)

if name=="main": app = Caluator() app.title("科学计算器") app.geometry("280x400+120+200") app.mainloop()

直接运行,会输出 7,但是点击 7 这个按钮,不会输出 7,请问是哪里出了问题?

1535 次点击
所在节点    Python
2 条回复
no1xsyzy
2021-04-06 21:47:05 +08:00
bind 的第二个参数应是一个函数,且在每次点击按钮时被按下
但你这里是一旦启动会调用一次 self.caluator_func(7),把得到的 None 绑定到按钮上
可以 button7.bind("<Button-1>", functools.partial(self.caluator_func, 7))
或者 lambda,或者为每个按钮单独设置。
tars2018
2021-04-07 09:15:41 +08:00
@no1xsyzy 谢谢,主要是想获取获取按钮的 text 属性,已解决。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/768481

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX