2017-03-27 160 views
0

所以我知道问题是什么,我只是不知道如何解决它: self.health获取存储在变量一次,并在此之后不重新读取。我试过使用:@property。但我只是昨天才了解到@property,所​​以无论是1:我没有正确使用它,或者2:在这种情况下不能使用它。tkinter按钮与点击时更改txt

import tkinter as tk 


class button_health: 
    def __init__(self): 
     self.health = 5 
    def hit(self, event): 
     self.health -= 1 
bob = button_health() 

window = tk.Tk() 
button = tk.Button(window, text = bob.health #I want this to update) 
button.bind("<Button-1>", bob.hit) 
button.pack() 

window.mainloop() 

什么我的目标是使代码在屏幕上显现简单的Tkinter按钮,开始了说“5”,那么当你点击它,说:“4”,然后单击“3”等

回答

1

使用的Tkinter IntVar变量来跟踪变化的健康值。使用textvariable属性将该变量挂钩到按钮标签。

import tkinter as tk 

class button_health: 
    def __init__(self, health=5): 
     self.health = tk.IntVar() 
     self.health.set(health) 

    def hit(self, event=None): 
     if self.health.get() >= 1: # assuming that you can't have negative health 
      self.health.set(self.health.get() - 1) 

window = tk.Tk() 
bob = button_health(8) 
button = tk.Button(window, textvariable=bob.health, command=bob.hit) 
#button = tk.Button(window, textvariable=bob.health) 
#button.bind('<Button-1>', bob.hit) 
button.pack() 

window.mainloop() 

另一种方法是创建自己的按钮类为Button一个子类,挂钩一个IntVar作为类中的一员。通过这种方式,您可以轻松创建多个具有不同健康值的独立按钮:

import tkinter as tk 

class HealthButton(tk.Button): 
    def __init__(self, window, health=5, *args, **kwargs): 
     self.health = tk.IntVar() 
     self.health.set(health) 
     super(HealthButton, self).__init__(window, *args, textvariable=self.health, command=self.hit, **kwargs) 

    def hit(self): 
     if self.health.get() >= 1: 
      self.health.set(self.health.get() - 1) 

window = tk.Tk() 
buttons = [HealthButton(window, i) for i in range(10,15)] 
for b in buttons: 
    b.pack() 

window.mainloop() 
1

tk.Button中有一个参数command。将button-1绑定到函数不是检查用户是否单击该按钮的最佳方法。即使你使用绑定,它应该绑定到一个函数,而不是一个类。 要更改按钮的文本,您可以将button['text']设置为某种内容。

import tkinter as tk 


def button_health(): 
    global health 
    health -= 1 
    button['text'] = str(health) 

health = 5 
window = tk.Tk() 
button = tk.Button(window, text = health , command = button_health) 
button.pack() 

window.mainloop() 

您还可以避免使用global声明这样做:

import tkinter as tk 

def button_health(but): 
    but.health -= 1 
    but['text'] = str(but.health) 

window = tk.Tk() 
button = tk.Button(window) 
button.health = 5 
button['text'] = str(button.health) 
button['command'] = lambda: button_health(button) 
button.pack() 

window.mainloop() 

对于做这种方式的另一个优点是,它可以保持按键独立的健康状况,因此,如果您有多个按钮,这将保持所有按钮的计数器不同。

+0

Thanks :)。我将不得不做一点点的学习来完全理解你的回答(我从来没有用过lambda)。但看起来不错。 我努力遵循第二个解决方案的语法。我仍然很喜欢python。 button.health = 5#我以前没有遇到过这种语法。我不明白这是什么。它是否在按钮中创建了一个名为“健康”的变量并将其分配给5? –

+0

'button.health = 5'在'button'中创建一个名为'health'的*属性*。这样,跟踪谁的健康状况会更容易。 'button.health'的'health'对于'button'永远是唯一的。 – abccd

+0

而在我的情况下,lambda保持'button_health(button)'在'command'被定义之后执行。 – abccd

1

使用button['text']button.config(text={text})

class button_health: 
    def __init__(self): 
     self.health = 5 
    def hit(self, event): 
     self.health -= 1 
     button['text'] = str(self.health) 

class button_health: 
    def __init__(self): 
     self.health = 5 
    def hit(self, event): 
     self.health -= 1 
     button.config(text= str(self.health))