당신은 전역 변수를 생성하고 기능 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
필요하지 않습니다.
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()
나는 내가 다른
IntVar
많은 버튼
countdown
을 사용할 수 있습니다
x
와
countdown()
을 실행하는 데
lambda
를 사용합니다. BTW
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
를 사용할 때 당신은 첫 번째 예에서 동일한 작업을 수행 할 수 없음) : 당신은 :)
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()
을 클릭하지 않고 기능
countdown
모든 1과 카운트 다운을 실행after(miliseconds, functio_name)
을 사용할 수 있습니다 왜 그 편집을 취소 했습니까? 믿을 수 없을만큼 분명하지 않은가? – jonrsharpe난 그냥 취소 했나요? 오, 하나님, 내 나쁜 생각은 그것이 제안이고 그것을 더 잘 coppied하고 그것을 만들려고 노력했다. –