【发布时间】:2017-09-15 18:35:12
【问题描述】:
我有一个连接到按钮的 arduino。按下按钮时,通过串行发送串行输出 1。我希望在按下按钮时启动 python tkinter 秒表。目前我知道 1 正在被 python 串行读取读取。并在 python 终端打印出来。但我无法控制 tkinter 秒表。 PS:我对python相当陌生。以下是我当前的代码。
from tkinter import *
import time
import serial
ser = serial.Serial(
port='COM4',\
baudrate=57600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=10)
print("connected to: " + ser.portstr)
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
def main():
root = Tk()
sw = StopWatch(root)
sw.pack(side=TOP)
root.mainloop()
if __name__ == '__main__':
main()
root = Tk()
sw = StopWatch(root)
sw.pack(side=TOP)
count=1
while True:
for line in ser.read():
print(chr(line))
count = count+1
if chr(line) == '1':
sw.Start()
ser.close()
编辑:根据我的评论回答,我已经开始工作了。下面是工作代码。
from tkinter import *
import time
import serial
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
def Read():
ser = serial.Serial(
port='COM4',\
baudrate=57600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=10)
print("connected to: " + ser.portstr)
count=1
while True:
for line in ser.read():
print(chr(line))
count = count+1
return chr(line)
ser.close()
def main():
root = Tk()
sw = StopWatch(root)
sw.pack(side=TOP)
ser = Read()
if ser == '1':
sw.Start()
root.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
-
一个明显的问题是
sw.Start没有调用该函数。它必须是sw.Start()。 -
@BryanOakley 我已经更新了我的代码,因为 gui 无法启动。现在 gui 启动但秒表不会启动。当我关闭 gui 时,命令行界面显示 sw 未定义。
-
那么你的缩进在你的
if __name__ == '__main__':声明中是关闭的 -
@SierraMountainTech 怎么样?另外,如果我将 root = Tk() sw = StopWatch(root) sw.pack(side=TOP) 放在 while true 语句之上,什么也不会发生
-
count=1while True:声明和ser.close()都应该与main()一致,而且除非你的意图,否则它甚至都不会运行,直到你的应用程序关闭。
标签: python tkinter arduino pyserial