-
Hello, Can you make the program as one file exe using pyinstaller and custom error images? I tried it, but once I use custom images and give the location to those files, they work on the PC where the files are located, but once I move the location of the files or give the program to someone else, the pop ups won't appear (they appear in the background are are blank) Thank you! ^^ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 21 replies
-
@nekoalin You should change the location search method. So you should change the method for searching files.
Inside program.py: asset = "folder/image.png" ‣ best way to search the asset: import os
base_path = os.path.dirname(os.path.realpath(__file__))
asset = os.path.join(base_path, "folder", "image.png") You should use this method to search files and images and it will never give errors. Secondly, if you are making a one-file exe then you have to keep that folder outside the exe. If you are packing them inside, then you have to change the search method again. That search method and a detailed guide is given here: TomSchimansky/CustomTkinter#939 |
Beta Was this translation helpful? Give feedback.
-
Don't save it to the asset folder as it is packed inside the one-file exe, one-file exe treats that as a temporary folder so it will delete it after exiting the program and create a new one on every startup. home_dir = os.getenv("UserProfile")
save_file = os.path.join(home_dir, '.example_app', 'file.ini')
save_config can be called when you exit the program. (Otherwise, you have to implement a separate button for it) Here is a full example for your reference: import customtkinter
from configparser import ConfigParser
import os
def load():
if not os.path.exists(save_file):
return
config = ConfigParser()
config.read(save_file)
# edit widgets based on the config file data
if config.get('switch', 'toggle')=='1':
switch.select()
set_mode()
textbox.insert(0.0, config.get('textbox', 'string'))
combobox.set(config.get('combobox', 'option'))
def save_on_exit():
print(f"saved file: {save_file}") # print the saved file location
# check if base folder exists
if not os.path.exists(os.path.join(home_dir, '.example_app')):
os.mkdir(os.path.join(home_dir, '.example_app'))
# set values to sections
config = ConfigParser()
config.add_section('switch')
config.set('switch', 'toggle', str(switch.get()))
config.add_section('textbox')
config.set('textbox', 'string', textbox.get(0.0, customtkinter.END))
config.add_section('combobox')
config.set('combobox', 'option', combobox.get())
# save the file
with open(save_file, 'w') as f:
config.write(f)
app.destroy()
def set_mode():
if switch.get()==1:
customtkinter.set_appearance_mode("Dark")
else:
customtkinter.set_appearance_mode("Light")
# save the file in the user folder
home_dir = os.getenv("UserProfile")
save_file = os.path.join(home_dir, '.example_app', 'file.ini')
app = customtkinter.CTk()
app.title("Example App")
app.geometry("250x300")
# save file on exit
app.protocol("WM_DELETE_WINDOW", save_on_exit)
# some widgets
frame_1 = customtkinter.CTkFrame(master=app)
frame_1.pack(expand=True, fill="both")
switch = customtkinter.CTkSwitch(master=frame_1, text="Dark Mode", command=set_mode)
switch.pack(pady=10)
set_mode()
textbox = customtkinter.CTkTextbox(master=frame_1)
textbox.pack()
combobox = customtkinter.CTkComboBox(master=frame_1, values=["A", "B", "C"])
combobox.pack(pady=10)
load() # load the data
app.mainloop() |
Beta Was this translation helpful? Give feedback.
@nekoalin You should change the location search method.
I guess you are finding assets like this:
C:/some_folder/.../image.png
This is not a good way to search files because the location may not exist in other computers.
So you should change the method for searching files.
For example, this is your folder structure:
Inside program.py:
‣ simple way to search that asset:
‣ best way to search the asset:
You should use this method to search files and images and it will never give errors.
Secondly, if you are ma…