как обновить значение после нажатия кнопки в ткинтере - PullRequest
0 голосов
/ 28 мая 2018

Я пробовал это, но значение не обновляется после каждого нажатия кнопки

import tkinter as tk

win = tk.Tk()
max_amount = 0

def fun():
    global max_amount
    max_amount +=100

btn = tk.Button(win,text = 'Change', command = fun)
btn.grid()
t1 =str(max_amount)
label1 = tk.Label(win,text = 'Balance :$' + t1)
label1.grid()

win.mainloop()

Ответы [ 2 ]

0 голосов
/ 28 мая 2018
import tkinter as tk

win = tk.Tk()
max_amount = 0

def fun():
    global max_amount
    max_amount +=100
    # use the config option to change the text of the label1 
    label1.config(text="Balance :$ "+str(max_amount))


btn = tk.Button(win,text = 'Change', command = fun)
btn.grid()
label1 = tk.Label(win, text = 'Balance :$0')
label1.grid()

win.mainloop()
0 голосов
/ 28 мая 2018
import tkinter as tk

win = tk.Tk()
max_amount = 0
label1 = None #just so it is defined

def fun():
    global max_amount, label1
    max_amount +=100
    label1.configure(text='Balance :$' + str(max_amount))

btn = tk.Button(win,text = 'Change', command = fun)
btn.grid()
t1 =str(max_amount)
label1 = tk.Label(win,text = 'Balance :$' + t1)
label1.grid()

win.mainloop()

Вам необходимо перенастроить метку.Это должно сделать так.Я надеюсь, что это помогло.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...