日常工作中,我们需要经常插拔串口,每次都要进入设备管理器查看串口号,再去配置串口,这样不太方便,因此写了个桌面浮窗工具,实时更新串口号。
在具体实现过程中,遇到多线程问题,用tkinter 自带的root.after()方法实现,代码如下:
```python
import serial.tools.list_ports
from tkinter import Tk, Canvas, Menu
from tkinter import W,LEFT
class DesktopGUI:
def __init__(self):
self.width = 100
self.height = 200
self.mainwin = Tk()
self.mainwin.title("串口列表")
#self.mainwin.iconbitmap(logopath)
self.mainwin.overrideredirect(True) #去掉边框
self.mainwin.wm_attributes('-topmost',1) #设置置顶
self.mainwin.attributes("-alpha", 0.5)#窗口透明度60 %
WinWidth=self.mainwin.winfo_screenwidth()
WinHeight=self.mainwin.winfo_screenheight()
root_x=WinWidth-self.width
root_y=WinHeight-self.height-50
self.mainwin.geometry(f"{self.width}x{self.height}+{root_x}+{root_y}")
self.MouseX=None
self.MouseY=None
self.canvas = Canvas(self.mainwin)
self.canvas.configure(width = self.width)
self.canvas.configure(height = self.height)
#canvas.configure(bg = "red")
self.canvas.configure(highlightthickness = 0)
self.menu = Menu(self.canvas,tearoff=0)
self.menu.add_command(label="刷新串口", command=self.RefreshPort)
self.menu.add_separator()
self.menu.add_command(label="退出", command=self.mainwin.quit)
#filename = tkinter.PhotoImage(file = "ai_1.gif")
#canvas.create_image(150,150, image=filename)
self.canvas.bind("<Button-1>",self.LeftButton)
self.canvas.bind("<B1-Motion>",self.Motion)
self.canvas.bind("<Button-3>",self.RightButton)
self.canvas.pack()
self.RefreshEnable = 1
self.PortRefreshLoop()
def Motion(self,event):
new_x = (event.x - self.MouseX)+self.mainwin.winfo_x()
new_y = (event.y - self.MouseY)+self.mainwin.winfo_y()
s = f"{self.width}x{self.height}+" + str(new_x)+"+" + str(new_y)
self.mainwin.geometry(s)
#print("当把我放到左上角200*200的区域时我会走人的,当前是x:%s,y:%s"%(new_x,new_y))
if new_x<50 and new_y<50:
exit()
def LeftButton(self,event):
self.MouseX,self.MouseY = event.x,event.y
print("event.x, event.y = ",event.x,event.y)
#右键菜单设置
def RightButton(self,event):
print(event.x_root, event.y_root)
self.menu.post(event.x_root, event.y_root)
def RefreshPort(self):
self.canvas.delete('all')
port_list = list(serial.tools.list_ports.comports())
port_str_list = [] # 用来存储切割好的串口号
if len(port_list)==0:
self.canvas.create_text((20, 20),text = 'None',
anchor = W,
justify = LEFT)
for i in range(len(port_list)):
# 将串口号切割出来
lines = str(port_list[i])
str_list = lines.split(" ")
port_str_list.append(str_list[0])
for i, st in enumerate(port_str_list):
#self.canvas.create_text((20, 20+i*20),text = st,
self.canvas.create_text((20, (self.height-20*len(port_str_list))/2+i*20),text = st,
anchor = W,
justify = LEFT)
def PortRefreshLoop(self):
if self.RefreshEnable:
self.RefreshEnable = 0
self.RefreshPort()
self.RefreshEnable = 1
self.mainwin.after(100,self.PortRefreshLoop)
def show(self):
self.mainwin.mainloop()
if __name__ == '__main__':
GUI = DesktopGUI()
GUI.show()```
Reference: CSDN