窗口实例化

2021-06-22 14:29:34 浏览数 (1)

  1. #!/usr/bin/env python
  2. #-*-coding:GBK -*-
  3. from tkinter import *
  4. import hashlib
  5. import time
  6. LOG_LINE_NUM = 0
  7. class MY_GUI():
  8. def __init__(self,init_window_name):
  9. self.init_window_name = init_window_name
  10. #设置窗口
  11. def set_init_window(self):
  12. self.init_window_name.title("wenbenchuli_v1.2") #窗口名
  13. #self.init_window_name.geometry('320x160 10 10') #290 160为窗口大小, 10 10 定义窗口弹出时的默认展示位置
  14. self.init_window_name.geometry('1068x681 10 10')
  15. #self.init_window_name["bg"] = "pink" #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
  16. #self.init_window_name.attributes("-alpha",0.9) #虚化,值越小虚化程度越高
  17. #标签
  18. self.init_data_label = Label(self.init_window_name, text="daichuli")
  19. self.init_data_label.grid(row=0, column=0)
  20. self.result_data_label = Label(self.init_window_name, text="输出结果")
  21. self.result_data_label.grid(row=0, column=12)
  22. self.log_label = Label(self.init_window_name, text="日志")
  23. self.log_label.grid(row=12, column=0)
  24. #文本框
  25. self.init_data_Text = Text(self.init_window_name, width=67, height=35) #原始数据录入框
  26. self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
  27. self.result_data_Text = Text(self.init_window_name, width=70, height=49) #处理结果展示
  28. self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
  29. self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
  30. self.log_data_Text.grid(row=13, column=0, columnspan=10)
  31. #按钮
  32. self.str_trans_to_md5_button = Button(self.init_window_name, text="字符串转MD5", bg="lightblue", width=10,command=self.str_trans_to_md5) # 调用内部方法 加()为直接调用
  33. self.str_trans_to_md5_button.grid(row=1, column=11)
  34. #功能函数
  35. def str_trans_to_md5(self):
  36. src = self.init_data_Text.get(1.0,END).strip().replace("n","").encode()
  37. #print("src =",src)
  38. if src:
  39. try:
  40. myMd5 = hashlib.md5()
  41. myMd5.update(src)
  42. myMd5_Digest = myMd5.hexdigest()
  43. #print(myMd5_Digest)
  44. #输出到界面
  45. self.result_data_Text.delete(1.0,END)
  46. self.result_data_Text.insert(1.0,myMd5_Digest)
  47. self.write_log_to_Text("INFO:str_trans_to_md5 success")
  48. except:
  49. self.result_data_Text.delete(1.0,END)
  50. self.result_data_Text.insert(1.0,"字符串转MD5失败")
  51. else:
  52. self.write_log_to_Text("ERROR:str_trans_to_md5 failed")
  53. #获取当前时间
  54. def get_current_time(self):
  55. current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
  56. return current_time
  57. #日志动态打印
  58. def write_log_to_Text(self,logmsg):
  59. global LOG_LINE_NUM
  60. current_time = self.get_current_time()
  61. logmsg_in = str(current_time) " " str(logmsg) "n" #换行
  62. if LOG_LINE_NUM <= 7:
  63. self.log_data_Text.insert(END, logmsg_in)
  64. LOG_LINE_NUM = LOG_LINE_NUM 1
  65. else:
  66. self.log_data_Text.delete(1.0,2.0)
  67. self.log_data_Text.insert(END, logmsg_in)
  68. def gui_start():
  69. init_window = Tk() #实例化出一个父窗口
  70. ZMJ_PORTAL = MY_GUI(init_window)
  71. # 设置根窗口默认属性
  72. ZMJ_PORTAL.set_init_window()
  73. init_window.mainloop() #父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示
  74. gui_start()

0 人点赞