2016-11-14 81 views
-3

我使用tkinter来显示格式为300.4的传感器信息,我在想如何编码它,因此变量的值将显示在GUI上?Tkinter,显示一个变量

难道是简单的:

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

任何基本的tkinter教程都应该涵盖这一点。请搜索一个并关注它。 – Lafexlos

回答

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() 
+0

你可以试试这个代码。希望你有一个想法如何从一个变量在窗口上显示文本。 :) – Inconnu

+0

它应该工作,让我知道如果它不。 – Inconnu

-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设置为一个随机数,并在底部指示符处显示文本。

+2

是您的问题的解决方案吗?如果不是,则删除它或置疑。 SO是不正常的论坛。顺便说一句,使用master.after()而不是'while while True' – furas