- 设置ico
首先生成一个ico文件,可以通过https://www.bitbug.net在线通过图片生成
其次将ico文件通过base64编码,转成一个ico.py资源文件
import base64
open_icon = open("ico.ico","rb")
b64str = base64.b64encode(open_icon.read())
open_icon.close()
write_data = 'img=%s' % b64str
f = open("ico.py","w")
f.write(write_data)
f.close()
再次在程序中引用ico.py,并解码图片并展示,从而达到编译时有ico
from tkinter import *
from tkinter.ttk import *
import os
from ico import img
import base64
def app_gui_start():
init_window = Tk()
# 设置窗口最大化按钮不可用
init_window.resizable(0,0)
#程序运行时在屏幕中间打开
sw = init_window.winfo_screenwidth()
sh = init_window.winfo_screenheight()
ww = 600
wh = 400
x = (sw-ww) / 2
y = (sh-wh) / 2
init_window.title("app client")
init_window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
# 设置窗口的图标和编译exe的图标
tmp = open("tmp.ico","wb+")
tmp.write(base64.b64decode(img))
tmp.close()
init_window.iconbitmap("tmp.ico")
os.remove("tmp.ico")
# 窗口其他属性初始化
# api_PORTAL = app_GUI(init_window)
# api_PORTAL.set_init_window()
init_window.mainloop()
app_gui_start()
效果:
- 调整背景色
from tkinter import *
from tkinter.ttk import *
from tkinter import scrolledtext
from os import environ
from os.path import join, exists
class app_GUI():
def __init__(self,init_window_name):
self.init_window_name = init_window_name
def set_init_window(self):
self.theme = -1
self.set_frame_lt()
self.load_theme()
self.change_theme()
def set_frame_lt(self):
self.frmLT = Frame(width=500,height=400)
self.frmLT.grid(row=0, column=0, padx=(18,0)) #左侧间隔18
frmLT = self.frmLT
self.init_data_label = Label(frmLT, text="init data")
self.init_data_label.grid(row=1, column=0, columnspan=15, pady=(12,0)) #上面间隔12
self.init_data_Text = scrolledtext.ScrolledText(frmLT,width=78, height=20,borderwidth=0.1) #borderwidth=0.1 去除边框
self.init_data_Text.grid(row=2, column=0, columnspan=15,sticky=NW, pady=12)
self.radiovar = IntVar()
self.R1 = Radiobutton(frmLT, text="value1", variable=self.radiovar, value=1)#, command=self.func1)
self.R1.grid(row=3, column=5)
self.R2 = Radiobutton(frmLT, text="value2", variable=self.radiovar, value=2)#, command=self.func1)
self.R2.grid(row=3, column=10)
self.radiovar.set(1)
self.linkLoop_button = Button(frmLT, text="theme",command=self.change_theme)
self.linkLoop_button.grid(row=4, columnspan=15, pady=(12,0))
def change_theme(self):
colors = {
0 : "#EFEBE7",
1 : "#F9CDDC",
2 : "#C578A4",
3 : "#9B7EB6",
4 : "#A8B680",
5 : "#F9DDD3",
6 : "#848786",
}
self.theme+=1
self.change_ele_bg(colors[self.theme%len(colors)])
self.save_theme()
def change_ele_bg(self, themecolor):
gui_style = Style()
gui_style.configure('My.TRadiobutton', background=themecolor)
gui_style.configure('My.TFrame', background=themecolor)
self.init_window_name['bg'] = themecolor #主窗口的背景色
self.frmLT['style']='My.TFrame' #frame的背景色
self.R1['style'] = 'My.TRadiobutton' #单选的背景色
self.R2['style'] = 'My.TRadiobutton'
self.init_data_label['background'] = themecolor #Label的背景色
def save_theme(self):
paras = {"theme":self.theme-1 }
with open(self.theme_file(), 'w') as fd:
fd.write(str(paras))
def load_theme(self):
if not exists(self.theme_file()):
return
try:
with open(self.theme_file()) as fd:
paras = eval(fd.read())
self.theme = paras['theme']
except Exception as e:
print("ERROR: %s" % e)
pass
def theme_file(self):
return join(environ["appdata"], 'app_theme.ini')
def app_gui_start():
init_window = Tk()
# 设置窗口最大化按钮不可用
init_window.resizable(0,0)
#程序运行时在屏幕中间打开
sw = init_window.winfo_screenwidth()
sh = init_window.winfo_screenheight()
ww = 600 #app宽度
wh = 400 #app高度
x = (sw-ww) / 2
y = (sh-wh) / 2
init_window.title("app client")
init_window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
# 窗口其他属性初始化
api_PORTAL = app_GUI(init_window)
api_PORTAL.set_init_window()
init_window.mainloop()
app_gui_start()
效果图: