【问题标题】:Tkinter Button Binded to Keyboard Won't Count Class ParameterTkinter 按钮绑定到键盘不会计算类参数
【发布时间】:2020-11-28 07:01:29
【问题描述】:

我正在尝试创建函数'count',它以变量的形式接收一个整数,每次按下返回键时都加1,每次都保存到变量中。

参数需要保持通用,因为将来这将根据按下的按钮对多个变量运行相同的“计数”函数。

我尝试通过将global messi 放在顶部来使messi 成为全局变量,但出现了同样的问题。

import tkinter as tk

class PlayerStats:
    def __init__(self, name, touches):
        team = "Barcelona"
        self.name = name
        self.touches = touches
        
    def count(number):
        number = number + 1
        print(number)

messi = PlayerStats("Messi",0)

root = tk.Tk()
root.bind('<Return>', lambda event :PlayerStats.count(messi.touches))
root.mainloop()

当我运行这个 sn-p 时,它会迭代一次,从 0 到 1,然后重置总是打印出 1。

任何关于为什么会发生这种情况以及如何解决的想法将不胜感激!

【问题讨论】:

    标签: python python-3.x oop tkinter keyboard


    【解决方案1】:

    您没有保存操作的结果。

    您正在实例化您的 PlayerStats 类,touches 的值为 0。 然后,该值永远不会在您的代码中发生变化。

    当 tkinter 调用您的 count 方法时,它会递增 number 但该变量永远不会离开该方法的范围,因此会被垃圾回收。

    要修复它,您应该将您的课程更改为类似

    import tkinter as tk
    
    class PlayerStats:
        def __init__(self, name, touches):
            team = "Barcelona"
            self.name = name
            self.touches = touches
            
        def count(self):  # the first argument of a method is always a reference to the instance
            self.touches += 1
            print(self.touches)
    
    messi = PlayerStats("Messi", 0)
    
    root = tk.Tk()
    root.bind('<Return>', lambda event: messi.count())  # You need to call the method on the instance you created.
    root.mainloop()
    

    【讨论】:

    • 不要将 python 代码编辑为 sn-ps ,只需将其格式化为代码,用三个反引号或 ctrl + k 括起来,或者只是复制并粘贴它们。
    • 感谢您的帮助,这使我能够迭代变量并将其保存在正确的位置。
    【解决方案2】:

    感谢您的帮助 Dogeek。我上面的功能代码现在看起来像这样:

    import tkinter as tk
    
    class PlayerStats:
        def __init__(self, name, touches):
            team = "Barcelona"
            self.name = name
            self.touches = touches
            
        def count(self, number):
            self.touches += 1
            print(number)
    
    messi = PlayerStats("Messi",0)
    
    root = tk.Tk()
    root.bind('<Return>', lambda event :messi.count(messi.touches))
    root.mainloop()
    

    这并没有解决的一件事是能够为不同的变量重用该函数。我现在正试图想出一种优雅的方式来做这样的事情: 将 tkinter 导入为 tk

    class PlayerStats:
        def __init__(self, name, touches, shots):
            team = "Barcelona"
            self.name = name
            self.touches = touches
            self.shots = shots
    
        def count(self, number):
            self.number += 1
            print(number)
    
    messi = PlayerStats("Messi",0)
    
    root = tk.Tk()
    root.bind('<Return>', lambda event :messi.count(messi.touches))
    root.bind('<s>', lambda event :messi.count(messi.shots))
    root.mainloop()
    

    其中 number 代表 messi.shots 或 messi.touches 取决于按下的键。我不想为每个键重新创建一堆几乎相同的功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2020-04-08
      • 1970-01-01
      • 2018-05-08
      相关资源
      最近更新 更多