Advertisement
mod4

COM lister

Jan 20th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import threading
  2. import time
  3. from tkinter import *
  4. from tkinter import ttk
  5. from serial.tools import list_ports
  6.  
  7.  
  8. class App(ttk.Frame):
  9.     def __init__(self, root, stringVar):
  10.         """ Initialize the Frame"""
  11.         ttk.Frame.__init__(self, root)
  12.         self.root = root
  13.         self.stringvar = stringVar
  14.         threading.Thread.__init__(self)
  15.         self.grid()
  16.         self.label = ttk.Label(mainframe, textvariable=coms).grid(column=0, row=0, sticky=(W, E))
  17.         self.run()
  18.  
  19.     def run(self):
  20.         loop_active = True
  21.         while loop_active:
  22.             self.updatePorts()
  23.             self.root.update()
  24.             time.sleep(0.1)
  25.  
  26.     def updatePorts(self):
  27.         self.list = list_ports.comports()
  28.         self.portList = ""
  29.         for port in self.list:
  30.             self.portList += "{}\t{}\n".format(port.device, port.description)
  31.         self.stringvar.set(self.portList)
  32.  
  33.  
  34. root = Tk()
  35. root.title("COM")
  36. coms = StringVar()
  37. mainframe = ttk.Frame(root, padding="3 3 12 12")
  38. mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
  39. root.columnconfigure(0, weight=1)
  40. root.rowconfigure(0, weight=1)
  41. app = App(root, coms)
  42. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement