I'm trying to use the tksleep way of doing time.sleep in my python script, but can't figure out how to use it outside of my gui.py file without having to add root = tk.Tk()
and root.tksleep(2)
to each one of my scripts, which will create a new tkinter window for every time I run or add root.tksleep(2)
.
I know .after is the normal way to delay events, but my scripts are in an order that they can take different amounts of time depending on what happens on the screen, which is why I can't predict how long the delay will be.
For example, if I run the script below called test1.py, it will work but will create an extra (custom)tkinter window, outside of the Toplevel window I already had from my gui.py file. I've thought of adding all the code together of my scripts into the gui.py file but that is not really the way I want to have my project organized.
test1.py:
import tkinter as tk
root = tk.Tk()
def tksleep(self, time:float) -> None:
self.after(int(time*1000), self.quit)
self.mainloop()
print("before waiting")
root.tksleep(1)
print("after waiting")
gui.py:
import customtkinter as ctk
import sys
import main
root = ctk.CTkToplevel()
# --- main ---
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
root.geometry("500x350")
root.iconbitmap(r"app.ico")
root.title('dux<3#0767')
frame = ctk.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
button = ctk.CTkButton(master=frame, text='Run', command=main.start)
button.pack(pady=12, padx=10)
root.mainloop()
1. I then tried to put the tksleep function into my gui.py file which, simplified, looked like this:
import tkinter as tk
import customtkinter as ctk
import sys
import main
root = ctk.CTkToplevel()
# --- functions ---
def tksleep(self, time:float) -> None:
self.after(int(time*1000), self.quit)
self.mainloop()
# --- main ---
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
root.geometry("500x350")
root.iconbitmap(r"app.ico")
root.title('dux<3#0767')
frame = ctk.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
button = ctk.CTkButton(master=frame, text='Run', command=main.start)
button.pack(pady=12, padx=10)
root.mainloop()
But upon importing gui.py into main.py, it was a circular import, because I imported main.py into gui.py too, for my button to start the program. (Gave me this error: AttributeError: partially initialized module 'main' has no attribute 'start' (most likely due to a circular import)
)
2. I've tried to make a class called sleep with a method called tksleep in my main.py file like this (which already creates another unnecessary extra tkinter window):
import tkinter as tk
class sleep():
root = tk.Tk()
def tksleep(self, time:float) -> None:
self.after(int(time*1000), self.quit)
self.mainloop()
and made my test.py file like this:
import main
print("before waiting")
main.sleep.tksleep(1)
print("after waiting")
which gave me the error: TypeError: tksleep() missing 1 required positional argument: 'time'
I'm quite new to this and may have forgotten to add any important information, in that case please notify me!Thank you a lot.