ui.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import random
  2. import tkinter
  3. import uuid
  4. from tkinter import *
  5. from tkinter.ttk import *
  6. from common.hardware import Hardware
  7. from config import conf
  8. class WinGUI(Tk):
  9. def __init__(self):
  10. super().__init__()
  11. self.__win()
  12. # self.tk_label_lab_api_base = self.__tk_label_lab_api_base(self)
  13. # self.tk_input_api_base = self.__tk_input_api_base(self)
  14. self.tk_label_lab_api_key = self.__tk_label_lab_api_key(self)
  15. self.tk_input_api_key = self.__tk_input_api_key(self)
  16. self.tk_input_token = self.__tk_input_token(self)
  17. self.tk_label_lab_token = self.__tk_label_lab_token(self)
  18. # self.tk_button_save = self.__tk_button_save(self)
  19. self.tk_button_start = self.__tk_button_start(self)
  20. self.tk_button_pause = self.__tk_button_pause(self)
  21. self.tk_button_version = self.__tk_button_version(self)
  22. def __win(self):
  23. self.title("AI员工")
  24. # 设置窗口大小、居中
  25. width = 670
  26. height = 180
  27. screenwidth = self.winfo_screenwidth()
  28. screenheight = self.winfo_screenheight()
  29. geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
  30. self.geometry(geometry)
  31. self.resizable(width=False, height=False)
  32. def scrollbar_autohide(self, vbar, hbar, widget):
  33. """自动隐藏滚动条"""
  34. def show():
  35. if vbar: vbar.lift(widget)
  36. if hbar: hbar.lift(widget)
  37. def hide():
  38. if vbar: vbar.lower(widget)
  39. if hbar: hbar.lower(widget)
  40. hide()
  41. widget.bind("<Enter>", lambda e: show())
  42. if vbar: vbar.bind("<Enter>", lambda e: show())
  43. if vbar: vbar.bind("<Leave>", lambda e: hide())
  44. if hbar: hbar.bind("<Enter>", lambda e: show())
  45. if hbar: hbar.bind("<Leave>", lambda e: hide())
  46. widget.bind("<Leave>", lambda e: hide())
  47. def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph):
  48. widget.configure(yscrollcommand=vbar.set)
  49. vbar.config(command=widget.yview)
  50. vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')
  51. def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph):
  52. widget.configure(xscrollcommand=hbar.set)
  53. hbar.config(command=widget.xview)
  54. hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')
  55. def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph):
  56. vbar, hbar = None, None
  57. if is_vbar:
  58. vbar = Scrollbar(master)
  59. self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
  60. if is_hbar:
  61. hbar = Scrollbar(master, orient="horizontal")
  62. self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
  63. self.scrollbar_autohide(vbar, hbar, widget)
  64. def __tk_label_lab_api_base(self, parent):
  65. label = Label(parent, text="API_BASE", anchor="center", )
  66. label.place(x=30, y=30, width=60, height=30)
  67. return label
  68. def __tk_input_api_base(self, parent):
  69. ipt = Entry(parent, )
  70. ipt.insert(0, conf().get("api_base"))
  71. ipt.place(x=100, y=30, width=520, height=30)
  72. return ipt
  73. def __tk_label_lab_api_key(self, parent):
  74. label = Label(parent, text="设备号", anchor="center", )
  75. label.place(x=30, y=30, width=60, height=30)
  76. return label
  77. def __tk_input_api_key(self, parent):
  78. ipt = Entry(parent, )
  79. merchine_code = Hardware.get_machine_code()
  80. ipt.insert(0, 'PC-'+str(merchine_code))
  81. ipt.place(x=100, y=30, width=520, height=30)
  82. ipt.config(state='readonly')
  83. return ipt
  84. def __tk_input_token(self, parent):
  85. ipt = Entry(parent, )
  86. ipt.place(x=100, y=80, width=520, height=30)
  87. return ipt
  88. def __tk_label_lab_token(self, parent):
  89. label = Label(parent, text="TOKEN", anchor="center", )
  90. label.place(x=30, y=80, width=60, height=30)
  91. return label
  92. def __tk_button_save(self, parent):
  93. btn = Button(parent, text="保存", takefocus=False, )
  94. btn.place(x=30, y=190, width=80, height=30)
  95. return btn
  96. def __tk_button_start(self, parent):
  97. btn = Button(parent, text="启动", takefocus=False, )
  98. btn.place(x=30, y=130, width=80, height=30)
  99. return btn
  100. def __tk_button_pause(self, parent):
  101. btn = Button(parent, text="暂停", takefocus=False, state=tkinter.DISABLED )
  102. btn.place(x=130, y=130, width=80, height=30)
  103. return btn
  104. def __tk_button_version(self, parent):
  105. btn = Button(parent, text="版本", takefocus=False )
  106. btn.place(x=530,y=130, width=50, height=30)
  107. return btn