Tkinter,显示变量

时间:2016-11-14 06:13:23

标签: python tkinter tkinter-canvas

我正在使用tkinter来显示格式为300.4的传感器信息,我想知道如何对其进行编码,以便变量的值显示在GUI上?

它会如此简单:

x = sensor_value = 300.4
T = Text(root, height=2, width=30, text=x)

2 个答案:

答案 0 :(得分:0)

您应该至少显示您的代码,以表明您确实尝试过这样做。 任何通过互联网的教程都可以回答你的问题。 要了解它的外观:

from tkinter import *

sensor_value =300.4 #said sensor value
master = Tk()
x = sensor_value #assigned to variable x like you showed
master.minsize(width=400, height=400)
w = Label(master, text=x) #shows as text in the window
w.pack() #organizes widgets in blocks before placing them in the parent.          

mainloop()

答案 1 :(得分:-1)

我很抱歉我在另一台机器上,这是我的代码,我也试图为它制作动画,我很快就会请求帮助。

    #!/usr/bin/env python
import Tkinter
from Tkinter import *
import random
master = Tk()

canvas_width = 1000
canvas_height = 1000
w = Canvas(master, 
           width=canvas_width,
           height=canvas_height)


w.pack()
x = random.randint(0,900)
h = x + 2.5
b = x - 2.5

while True:
         w.create_rectangle(20, 45, 900, 50, fill='white')
         w.create_rectangle(20, 20, 25, 50, fill='white')
         w.create_rectangle(895, 20, 900, 80, fill='white')
         w.create_rectangle(445, 35, 450, 50, fill='white')
         w.create_text(700, 35, text="Electromagnetic Field, milliaTeslas", fill='white')
         w.create_text(900, 10, text="0", fill='white')
         w.create_rectangle(b, 50, h, 80, fill='red')
         w.update()

我将x设置为随机数,并在底部指示符处显示文本。