在 Tkinter 中刷新 gps 数据
Refreshing gps data in Tkinter
我有以下脚本,可将高度数据从 gpsd 模块输出到控制台并保持最新。我想在 tkinter 界面中显示相同的数据,但无论我尝试过什么,我似乎都无法让它像控制台那样刷新数据。它输出初始数据正常,但没有给我最新数据。我是一个新手所以也许我在做一些愚蠢的事情。
我附上了刚输出到控制台的初始代码,因为它是我的基础。
谢谢,
段.
import os
from gps import *
from time import *
import time
import threading
gpsd = None #seting the global variable
os.system('clear') #clear the terminal (optional)
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
os.system('clear')
print 'altitude (m)' , gpsd.fix.altitude
time.sleep(5) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print "Done.\nExiting."
假设 gpsd.next()
相对较快(不到 200 毫秒左右),您不需要线程来使其与 tkinter 一起工作。您可以使用事件循环定期 运行 一项工作。如果 gpsd.next()
花费很长时间,您可能需要使用线程或多处理,因为如果函数花费人类可察觉的时间到 运行.
以下示例显示如何使用 tkinter 事件循环进行轮询每秒更新数据。您可以通过将第一个参数更改为 self.after
来更改速度。我没有安装 gps 模块,所以我无法测试代码,但这给出了总体思路:
import Tkinter as tk
import gps
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.gps_label = tk.Label(self, text="", width=40)
self.gps_label.pack(fill="both", expand=True, padx=20, pady=20)
# initialize gps
self.gpsd = gps.gps(mode=WATCH_ENABLE)
self._update_gps_data()
def _update_gps_data(self):
data = self.gpsd.next()
self.gps_label.configure(text="gps data: " + data)
# run again in one second (1000 ms)
self.after(1000, self._update_gps_data)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
我有以下脚本,可将高度数据从 gpsd 模块输出到控制台并保持最新。我想在 tkinter 界面中显示相同的数据,但无论我尝试过什么,我似乎都无法让它像控制台那样刷新数据。它输出初始数据正常,但没有给我最新数据。我是一个新手所以也许我在做一些愚蠢的事情。
我附上了刚输出到控制台的初始代码,因为它是我的基础。
谢谢, 段.
import os
from gps import *
from time import *
import time
import threading
gpsd = None #seting the global variable
os.system('clear') #clear the terminal (optional)
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
os.system('clear')
print 'altitude (m)' , gpsd.fix.altitude
time.sleep(5) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print "Done.\nExiting."
假设 gpsd.next()
相对较快(不到 200 毫秒左右),您不需要线程来使其与 tkinter 一起工作。您可以使用事件循环定期 运行 一项工作。如果 gpsd.next()
花费很长时间,您可能需要使用线程或多处理,因为如果函数花费人类可察觉的时间到 运行.
以下示例显示如何使用 tkinter 事件循环进行轮询每秒更新数据。您可以通过将第一个参数更改为 self.after
来更改速度。我没有安装 gps 模块,所以我无法测试代码,但这给出了总体思路:
import Tkinter as tk
import gps
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.gps_label = tk.Label(self, text="", width=40)
self.gps_label.pack(fill="both", expand=True, padx=20, pady=20)
# initialize gps
self.gpsd = gps.gps(mode=WATCH_ENABLE)
self._update_gps_data()
def _update_gps_data(self):
data = self.gpsd.next()
self.gps_label.configure(text="gps data: " + data)
# run again in one second (1000 ms)
self.after(1000, self._update_gps_data)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()