ui.py 4.5 KB

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