V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tars2018
V2EX  ›  Python

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

  •  
  •   tars2018 · 2021-04-06 20:38:23 +08:00 · 1534 次点击
    这是一个创建于 1325 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个计算器程序

    -- 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,请问是哪里出了问题?

    2 条回复    2021-04-07 09:15:41 +08:00
    no1xsyzy
        1
    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
        2
    tars2018  
    OP
       2021-04-07 09:15:41 +08:00
    @no1xsyzy 谢谢,主要是想获取获取按钮的 text 属性,已解决。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1244 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:26 · PVG 07:26 · LAX 15:26 · JFK 18:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.