简体   繁体   English

Python 3:无法同时使用串行通信和Tkinter按钮

[英]Python 3: Can't get serial communication and a Tkinter button to work at the same time

I am new to python. 我是python的新手。 I am attempting to get serial input from an Arduino board and receive it in Python 3 on a Raspberry Pi 3. In the code below, I get the code just fine from the Arduino and I can display it using Tkinter. 我试图从Arduino板上获取串行输入并在Raspberry Pi 3上用Python 3接收它。在下面的代码中,我从Arduino获得的代码很好,我可以使用Tkinter显示它。 The thing is, depending on the code I get from the Arduino, I want to display different screens. 问题是,根据我从Arduino获得的代码,我想显示不同的屏幕。 For this, I have added a Tkinter button. 为此,我添加了一个Tkinter按钮。 This button should just call the NextButton subroutine and increment the DisplayScreen value. 此按钮应该只调用NextButton子例程并递增DisplayScreen值。 It should then recall the ShowDisplay routine and give me the next screen. 然后它应该调用ShowDisplay例程并给我下一个屏幕。 The button does display on the screen, but clicking it does nothing. 该按钮确实显示在屏幕上,但单击它不会执行任何操作。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

import serial
from tkinter import *
v=" "
DisplayScreen =1
# Make Serial Connection
ser = serial.Serial('/dev/ttyACM0', 9600)
#Subroutine to increment display value
def NextButton():
    DisplayScreen = DisplayScreen +1
    Print ("Got Here")
    if DisplayScreen == 3:
        DisplayScreen = 1
    # Update Display
    ShowDisplay()

#Subroutine to show display
def ShowDisplay():
    # Make values available from other parts of the program.
    global v    
    if DisplayScreen == 1:
        # Get rid of existing display entities
        for widget in frame.winfo_children():
            widget.destroy()
        #Add Label
        Label(frame, text="Display Screen 1").grid(row=0, column=0)
        Label(frame, text=v).grid(row=1, column=0)  
        # Add button to go to next screen
        Button(frame, text='Next', command=NextButton).grid(row=3, column=1)
    else:
        # Get rid of existing display entities
        for widget in frame.winfo_children():
            widget.destroy()
        #Add Label
        Label(frame, text="Display Screen 2").grid(row=0, column=0)
        Label(frame, text=v).grid(row=1, column=0)  
        # Add button to go to next screen
        Button(frame, text='Next', command=NextButton).grid(row=3, column=1)


def update_label():
    global v
    # get arduino info
    PinballData= ser.readline()
    # convert data to text
    v = str(PinballData, 'utf-8')
    # show display
    ShowDisplay()
    root.after(10, update_label)
    #calls update_label function again after 1 second. (1000 milliseconds.)

root = Tk()
frame = Frame(root)
frame.grid()
update_label()
root.mainloop()

There are several things that need fixing: 有几件事需要修复:

  • You should declare 'DisplayScreen' globally inside NextButton function. 您应该在NextButton函数中全局声明'DisplayScreen'。
  • Print statement is wrong. Print声明错了。 It must be print() 它必须是print()

Thus, the function NextButton has to look like: 因此,NextButton函数必须如下所示:

def NextButton():
    global DisplayScreen
    DisplayScreen = DisplayScreen +1
    print("Got Here")
    if DisplayScreen == 3:
        DisplayScreen = 1
    # Update Display
    ShowDisplay()

By doing these changes, I have been able to display "Got Here" by clicking on the button. 通过这些更改,我可以通过单击按钮显示“Got Here”。 However, it is really complicated to click on it since you are updating the entire graphical interface each 10 ms. 但是,由于每10毫秒更新整个图形界面,因此单击它非常复杂。

I would strongly recommend to update only the label instead of the entire root. 我强烈建议只更新标签而不是整个根。 Even better, you can change the text of a widget at any time by associating a StringVar() : 更好的是,您可以通过关联StringVar()随时更改窗口小部件的文本:

v = StringVar()
Label(master, textvariable=v).pack()

In this way, you would have a much more stable graphical interface. 通过这种方式,您将拥有更稳定的图形界面。

I would also recommend you to make use of classes since you have several variables shared between functions. 我还建议你使用类,因为你在函数之间共享了几个变量。 Then, you could easily use self . 然后,你可以轻松使用self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM