【问题标题】:Python Serial Connections with Tkinter Buttons带有 Tkinter 按钮的 Python 串行连接
【发布时间】:2020-01-13 23:58:55
【问题描述】:

我正在尝试为串行项目制作 GUI,我需要能够选择一个通讯端口并使用该通讯端口运行命令。

这是我想要的 GUI 示例,表格形式。

Start Connection | Dropdown of Comm Numbers | Button to run commands

我有一些我一直在处理的代码,但这让我很困惑。

from tkinter import *
from tkinter.ttk import *
import tkinter as tk
import serial
import ctypes
from functools import partial

def connect(ser):    #button that starts a connection, ser
    try:
        port = D.get()    #snags the number in the dropdown
        print(D.get())
        ser = serial.Serial(
                        port = "COM" +str(port),
                        baudrate=9600,
                    )
        print(ser.port)
    except:
        ctypes.windll.user32.MessageBoxW(0, "Fail", title, 0)
        ser.close()
    return ser

def running(ser):    #button that runs a ton of commands through ser
    P['value']=0
    ser.write(b'lottsacommands')
    P['value']=100
    root.update_idletasks()

root = tk.Tk()
bottomframe = Frame(root)    #frame for bottom of root window
bottomframe.pack(side=tk.BOTTOM)

D = StringVar(bottomframe)
P = Progressbar(bottomframe,orient=HORIZONTAL,length=100,mode='determinate')  #build a progress bar in the window 'P'
B = tk.Button(bottomframe,text='Begin Run In',command=partial(running, ser))     #build the button 'B'
B2 = tk.Button(bottomframe,text="Connect To Serial",command=partial(connect, ser))
T2 = tk.Text(bottomframe, height=1, width=7)    #box that says comm

P.pack()    #pack progress bar
B.pack(side=tk.RIGHT)       #formats the button
B2.pack(side=tk.LEFT)
T2.pack(side=tk.LEFT)       #format the ports dropdown

ports = {1,2,3,4,5,6,7,8,9}
DR = OptionMenu(bottomframe, D, *choices)   #creates the dropdown
DR.pack()

T2.insert(tk.END, 'COMM')    #makes the box say comm
tk.mainloop()    #runs the loop

我似乎无法将连接函数中的通信端口连接转移到第二个执行函数。任何帮助表示赞赏!

【问题讨论】:

  • 您发布的代码编译失败,因为ser 未定义。同样*choices 应该是*portsDR = OptionMenu(..., *choices)return ser 语句在 connect() 函数中没有用,因为它的返回值尚未在您的代码中使用。
  • 那么,如果我在代码的前面定义了 ser,比如ser = 1,return 语句会改变 ser 的值,以便下一个函数可以使用它?
  • 否,因为connect() 函数中的ser 是一个局部变量。您需要在connect() 中将其声明为全局。正如我之前所说,return ser 在这种情况下是无用的,因为没有返回的接收者。
  • 感谢您的纠正。我可以通过对 ser 使用全局变量来解决这个问题,以便将它带到函数之外。

标签: python function tkinter pyserial


【解决方案1】:

原来问题是我没有全局定义 ser。我更改了 connect 函数,使其创建的 ser 不再是函数的本地,而是全局的,覆盖了初始化 ser 的 1。

def connect(ser):    #button that starts a connection, ser
    try:
        global ser        #makes ser a global variable accessible to other functions
        port = D.get()    #snags the number in the dropdown
        print(D.get())
        ser = serial.Serial(
                        port = "COM" +str(port),
                        baudrate=9600,
                    )
        print(ser.port)
    except:
        ctypes.windll.user32.MessageBoxW(0, "Fail", title, 0)
        ser.close()
    return ser

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多