【问题标题】:Pyserial and tkinter not working?Pyserial 和 tkinter 不工作?
【发布时间】:2015-10-25 17:28:56
【问题描述】:

我目前正在开发一个需要串行通信和 GUI 的项目。我是 python 新手,所以我真的被卡住了,找不到任何关于此的信息,所以我想这是一个逻辑问题,但我无法弄清楚。非常感谢任何主题的帮助!

代码:

from tkinter import *
import serial, time, os

codeRead = open ('/home/riverans/Desktop/com/acticode.taws', 'r')
code = codeRead.read()
codeRead.close()

ser = serial.Serial('/dev/ttyACM0', 9600)

while 1:
    serialInput = ser.readline()

    if serialInput == b'1001\r\n':      #Read Input 1001
        file1 = open('/home/riverans/Desktop/com/I1.taws', 'w')
        file1.write(code)
        file1.close()

    if serialInput == b'1002\r\n':      #Read Input 1002
        if os.path.isfile('/home/riverans/Desktop/com/I1.taws'):
            os.remove('/home/riverans/Desktop/com/I1.taws')

    if serialInput == b'2001\r\n':      #Read Input 2001
        file2 = open('/home/riverans/Desktop/com/I2.taws', 'w')
        file2.write(code)
        file2.close()

    if serialInput == b'2002\r\n':      #Read Input 2002
        if os.path.isfile('/home/riverans/Desktop/com/I2.taws'):
            os.remove('/home/riverans/Desktop/com/I2.taws')

    if serialInput == b'3001\r\n':      #Read Input 3001
        file3 = open('/home/riverans/Desktop/com/I3.taws', 'w')
        file3.write(code);
        file3.close()

    if serialInput == b'3002\r\n':      #Read Input 3002
        if os.path.isfile('/home/riverans/Desktop/com/I3.taws'):
            os.remove('/home/riverans/Desktop/com/I3.taws')

    if os.path.isfile('/home/riverans/Desktop/com/I1.taws'):        #Write Pin 10
        ser.write(b'1')

    if os.path.isfile('/home/riverans/Desktop/com/I2.taws'):        #Write Pin 8
        ser.write(b'2')

    if os.path.isfile('/home/riverans/Desktop/com/I3.taws'):
        ser.write(b'3')

    print (serialInput)


class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    def init_window(self):
        self.master.title("MISSYS")
        self.pack(fill=BOTH, expand=1)

        menu = Menu(self.master)
        self.master.config(menu=menu)

        file = Menu(menu)
        file.add_command(label='Exit', command=self.client_exit)
        menu.add_cascade(label='File', menu=file)

        edit = Menu(menu)
        edit.add_command(label='Undo')
        menu.add_cascade(label='Edit', menu=edit)


        button1 = Button(self, text="Turn on LED", command=self.serial_write_led)
        button1.place(x=0, y=0)

        button2 = Button(self, text="Turn off LED", command=self.serial_write_led2)
        button2.place(x=100, y=0)

    def client_exit(self):
        exit()

    def serial_write_led(self):
        ser.write(b'1')
        print("Turning on LED")

    def serial_write_led2(self):
        ser.write(b'2')
        print("Turning off LED")

root = Tk()
root.geometry("500x300")

app = Window(root)

root.mainloop()

【问题讨论】:

  • 我想我刚刚在您的class Window 中更正的缩进是由于复制粘贴错误造成的。我建议指定 为什么 这不起作用;您不能简单地粘贴代码并期望其他用户神奇地发现错误。
  • 你能指出究竟是什么你的代码有问题吗?
  • @DimitrisJim 缩进不对。查看开头的while 循环。
  • 错过了,现在修好了。
  • 您的代码开头有一个无限循环。您将永远无法创建 Tk 对象。我不明白你到底想做什么,所以我无法建议如何纠正它。

标签: python python-3.x tkinter logic pyserial


【解决方案1】:

首先, 在使用 Tkinter GUI 时切勿使用无限循环或 time.sleep(ms),因为当您编写最后一行 top.mainloop() 时,tkinter 会创建自己的循环以保持应用程序处于活动状态,直到您手动关闭它。你的循环或睡眠功能会干扰 tkinter 的主循环

接下来 - 我发现如果您使用带有 tkinter 的 pyserial 模块,这也会导致问题。这是因为当你从串口读取时,它会让 python 等到串行缓冲区中有东西要读取。所以,我猜在内部某个地方有一个循环。

现在,我如何设法解决我的问题 - 我创建了两个 python 脚本。一个从串行端口获取数据,另一个显示 GUI。然后,我将这两个脚本分开但一起运行。第一个脚本从串行端口获取数据并转储到一个文件中,GUI 脚本读取同一个文件。因此我设法在 GUI 上获取串行数据。

这不是一个很好的方法,但我的目的已经解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多