当前位置:   article > 正文

【python】tkinter 设置ico和调整背景颜色_tkinter设置背景颜色默认

tkinter设置背景颜色默认
  •  设置ico

 

首先生成一个ico文件,可以通过https://www.bitbug.net在线通过图片生成

其次将ico文件通过base64编码,转成一个ico.py资源文件

  1. import base64
  2. open_icon = open("ico.ico","rb")
  3. b64str = base64.b64encode(open_icon.read())
  4. open_icon.close()
  5. write_data = 'img=%s' % b64str
  6. f = open("ico.py","w")
  7. f.write(write_data)
  8. f.close()

再次在程序中引用ico.py,并解码图片并展示,从而达到编译时有ico

  1. from tkinter import *
  2. from tkinter.ttk import *
  3. import os
  4. from ico import img
  5. import base64
  6. def app_gui_start():
  7. init_window = Tk()
  8. # 设置窗口最大化按钮不可用
  9. init_window.resizable(0,0)
  10. #程序运行时在屏幕中间打开
  11. sw = init_window.winfo_screenwidth()
  12. sh = init_window.winfo_screenheight()
  13. ww = 600
  14. wh = 400
  15. x = (sw-ww) / 2
  16. y = (sh-wh) / 2
  17. init_window.title("app client")
  18. init_window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
  19. # 设置窗口的图标和编译exe的图标
  20. tmp = open("tmp.ico","wb+")
  21. tmp.write(base64.b64decode(img))
  22. tmp.close()
  23. init_window.iconbitmap("tmp.ico")
  24. os.remove("tmp.ico")
  25. # 窗口其他属性初始化
  26. # api_PORTAL = app_GUI(init_window)
  27. # api_PORTAL.set_init_window()
  28. init_window.mainloop()
  29. app_gui_start()

效果:

  • 调整背景色
  1. from tkinter import *
  2. from tkinter.ttk import *
  3. from tkinter import scrolledtext
  4. from os import environ
  5. from os.path import join, exists
  6. class app_GUI():
  7. def __init__(self,init_window_name):
  8. self.init_window_name = init_window_name
  9. def set_init_window(self):
  10. self.theme = -1
  11. self.set_frame_lt()
  12. self.load_theme()
  13. self.change_theme()
  14. def set_frame_lt(self):
  15. self.frmLT = Frame(width=500,height=400)
  16. self.frmLT.grid(row=0, column=0, padx=(18,0)) #左侧间隔18
  17. frmLT = self.frmLT
  18. self.init_data_label = Label(frmLT, text="init data")
  19. self.init_data_label.grid(row=1, column=0, columnspan=15, pady=(12,0)) #上面间隔12
  20. self.init_data_Text = scrolledtext.ScrolledText(frmLT,width=78, height=20,borderwidth=0.1) #borderwidth=0.1 去除边框
  21. self.init_data_Text.grid(row=2, column=0, columnspan=15,sticky=NW, pady=12)
  22. self.radiovar = IntVar()
  23. self.R1 = Radiobutton(frmLT, text="value1", variable=self.radiovar, value=1)#, command=self.func1)
  24. self.R1.grid(row=3, column=5)
  25. self.R2 = Radiobutton(frmLT, text="value2", variable=self.radiovar, value=2)#, command=self.func1)
  26. self.R2.grid(row=3, column=10)
  27. self.radiovar.set(1)
  28. self.linkLoop_button = Button(frmLT, text="theme",command=self.change_theme)
  29. self.linkLoop_button.grid(row=4, columnspan=15, pady=(12,0))
  30. def change_theme(self):
  31. colors = {
  32. 0 : "#EFEBE7",
  33. 1 : "#F9CDDC",
  34. 2 : "#C578A4",
  35. 3 : "#9B7EB6",
  36. 4 : "#A8B680",
  37. 5 : "#F9DDD3",
  38. 6 : "#848786",
  39. }
  40. self.theme+=1
  41. self.change_ele_bg(colors[self.theme%len(colors)])
  42. self.save_theme()
  43. def change_ele_bg(self, themecolor):
  44. gui_style = Style()
  45. gui_style.configure('My.TRadiobutton', background=themecolor)
  46. gui_style.configure('My.TFrame', background=themecolor)
  47. self.init_window_name['bg'] = themecolor #主窗口的背景色
  48. self.frmLT['style']='My.TFrame' #frame的背景色
  49. self.R1['style'] = 'My.TRadiobutton' #单选的背景色
  50. self.R2['style'] = 'My.TRadiobutton'
  51. self.init_data_label['background'] = themecolor #Label的背景色
  52. def save_theme(self):
  53. paras = {"theme":self.theme-1 }
  54. with open(self.theme_file(), 'w') as fd:
  55. fd.write(str(paras))
  56. def load_theme(self):
  57. if not exists(self.theme_file()):
  58. return
  59. try:
  60. with open(self.theme_file()) as fd:
  61. paras = eval(fd.read())
  62. self.theme = paras['theme']
  63. except Exception as e:
  64. print("ERROR: %s" % e)
  65. pass
  66. def theme_file(self):
  67. return join(environ["appdata"], 'app_theme.ini')
  68. def app_gui_start():
  69. init_window = Tk()
  70. # 设置窗口最大化按钮不可用
  71. init_window.resizable(0,0)
  72. #程序运行时在屏幕中间打开
  73. sw = init_window.winfo_screenwidth()
  74. sh = init_window.winfo_screenheight()
  75. ww = 600 #app宽度
  76. wh = 400 #app高度
  77. x = (sw-ww) / 2
  78. y = (sh-wh) / 2
  79. init_window.title("app client")
  80. init_window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
  81. # 窗口其他属性初始化
  82. api_PORTAL = app_GUI(init_window)
  83. api_PORTAL.set_init_window()
  84. init_window.mainloop()
  85. app_gui_start()

效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/124834
推荐阅读
相关标签
  

闽ICP备14008679号