繁体   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