簡體   English   中英

如何使用python在Tkinter中打印變量?

[英]How to print variables in Tkinter using python?

place = Amsterdam
temp = 8.0

我想使用 Tkinter 制作一個Label ,打印出地點和溫度。 在常規代碼中,我會這樣做:

print(f'place: {place}, temperature: {temp}')

有沒有辦法使用 Tkinter 做到這一點?

提前致謝。

我試過:

label_1 = Label(text='place:' place )
label_1.pack()

但沒有運氣

對於Labeltext選項,您可以簡單地在print(...)使用相同的方式:

label_1 = Label(text=f'place: {place}, temperature: {temp}')
label_1.pack()

你必須做這樣的事情

root = tk.Tk() # this opens the window
place = tkinter.StringVar()
place.set("Amsterdam")
# here place your button
tkinter.Label(root, textvariable=place).pack() # this adds the label you want
root.mainloop() # this in order to loop the window

如果您不想將變量傳遞到標簽中,但希望標簽保持不變,您也可以這樣做

root = tk.Tk() # this opens the window
# here place your button
tkinter.Label(root, text="YourText").pack() # this adds the label you want
root.mainloop() # this in order to loop the window

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM