2017-03-27 159 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))