Python 与 Tkinter 按钮的串行连接
Python Serial Connections with Tkinter Buttons
我正在尝试为串行项目制作 GUI,我需要能够 select 通信端口和使用通信端口的 运行 命令。
这是我想要的 GUI 示例,采用 table 形式。
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
我似乎无法将连接函数中的 comm 端口连接转移到第二个执行函数。感谢您的帮助!
原来问题是我没有全局定义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
我正在尝试为串行项目制作 GUI,我需要能够 select 通信端口和使用通信端口的 运行 命令。
这是我想要的 GUI 示例,采用 table 形式。
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
我似乎无法将连接函数中的 comm 端口连接转移到第二个执行函数。感谢您的帮助!
原来问题是我没有全局定义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