Tkinter 实现桌面浮窗效果

日常工作中,我们需要经常插拔串口,每次都要进入设备管理器查看串口号,再去配置串口,这样不太方便,因此写了个桌面浮窗工具,实时更新串口号。
在具体实现过程中,遇到多线程问题,用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

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值