あなたはグローバル変数を作成し、機能countdown
でそれを使用する(ただし、global
を使用する必要があります)、その後、あなたはボタンのテキストを変更するためにconfig(text=...)
を使用することができます。
import Tkinter as tk
# --- functions ---
def countdown():
global x # use global variable
x -= 1
B.config(text=x)
# --- main ---
# create global variable (can be in any place - before/after function, before/after Tk())
x = 5
top = tk.Tk()
B = tk.Button(top, text=x, command=countdown)
B.pack()
top.mainloop()
それともIntVar()
とtextvariable
を使用することができます - あなたはconfig(text=...)
とglobal
を必要としません。私は、私は別のIntVar
で多くのボタンでcountdown
を使用することができますx
でcountdown()
を実行するためにlambda
を使用
import Tkinter as tk
# --- functions ---
def countdown():
x.set(x.get()-1)
# --- main ---
top = tk.Tk()
# create global variable (have to be after Tk())
x = tk.IntVar()
x.set(5)
# use `textvariable` and `lambda` to run `countdown` with `x`
B = tk.Button(top, textvariable=x, command=countdown)
B.pack()
top.mainloop()
。ところで
import Tkinter as tk
# --- functions ---
def countdown(var):
var.set(var.get()-1)
# --- main ---
top = tk.Tk()
# create global variable (have to be after Tk())
x = tk.IntVar()
x.set(5)
y = tk.IntVar()
y.set(5)
# use `textvariable` and `lambda` to run `countdown` with `x`
B1 = tk.Button(top, textvariable=x, command=lambda:countdown(x))
B1.pack()
B2 = tk.Button(top, textvariable=y, command=lambda:countdown(y))
B2.pack()
top.mainloop()
を(あなたがint
を使用するときは、最初の例では同じことを行うことはできません):あなたはクリックせず、すべて1とカウントダウン機能countdown
を実行するためにafter(miliseconds, functio_name)
を使用することができます:)
import Tkinter as tk
# --- functions ---
def countdown(var):
var.set(var.get()-1)
# run again time after 1000ms (1s)
top.after(1000, lambda:countdown(var))
# --- main ---
top = tk.Tk()
# create global variable (have to be after Tk())
x = tk.IntVar()
x.set(5)
# use `textvariable` and `lambda` to run `countdown` with `x`
B = tk.Button(top, textvariable=x)
B.pack()
# run first time after 1000ms (1s)
top.after(1000, lambda:countdown(x))
top.mainloop()
なぜその編集を元に戻したのですか?信じられないほど明白ではありませんでしたか? – jonrsharpe
私はそれを元に戻しましたか?ああ、私の悪い私はそれが提案だったと思って、それをより良く絡み合わせて作りました。 –