![](/img/trans.png)
[英]Using Tkinter in Python 2.6 how do I make a button widget print out selected Checkbuttons in a text box?
[英]How to make a button print out certain text? - Tkinter
下面,我創建了一個代碼來跟蹤正在跑步的人的圈數。 目前,我創建的按鈕跟蹤圈數和時間,但不在 GUI 窗口上。 我想知道如何使來自代碼的數據顯示在 GUI 窗口上。
from Tkinter import *
import time
laps=[0]
gui=Tk()
gui.geometry("200x100")
gui.title("lapping")
def afterbutton():
label_1=Label(gui,text=end())
def end():
end = time.time()
elapsed = end - start
laps[0]+=1
print "%s seconds" % (elapsed)
print "%s lap(s)" % (laps)
starting = raw_input("start? (s to start)")
if starting == "s":
start = time.time()
button1=Button(gui,command=afterbutton,text="lap")
button1.grid()
button1.pack()
gui.mainloop()
您需要在創建按鈕的同時創建標簽。 您需要將其放入網格並打包,就像您對按鈕所做的一樣。 然后當按鈕被點擊時,你所要做的就是更改標簽的文本。 這是它的樣子:
# python 3
from Tkinter import *
import time
laps=[0]
gui=Tk()
gui.geometry("200x100")
gui.title("lapping")
def end():
end = time.time()
elapsed = end - start
laps[0] += 1
s = "%s seconds %s laps" % (elapsed,laps)
label1['text'] = s
print("%s seconds" % (elapsed))
print("%s lap(s)" % (laps))
starting = raw_input("start? (s to start)")
if starting == "s":
start = time.time()
button1 = Button(gui, command=end, text="lap")
button1.grid()
button1.pack()
label1 = Label(gui)
label1.grid()
label1.pack()
gui.mainloop()
afterbutton
的功能afterbutton
了; 所有工作都由end()
。 我保留了你的大部分程序,但我使用的是 py3,所以 print 是一個函數而不是一個語句。 你必須為 py2 修復它。 標簽的文本由以下設置:
label1['text'] = s
當你第一次看到它時,這是一個有點奇怪的語法。
順便說一句,我不明白你為什么使用 1 元素列表來表示圈數而不是一個簡單的變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.