Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sv_ttk doesn't work with multiple tkinter.Tk instances #36

Closed
chiften1371 opened this issue Jul 9, 2022 · 4 comments
Closed

sv_ttk doesn't work with multiple tkinter.Tk instances #36

chiften1371 opened this issue Jul 9, 2022 · 4 comments
Labels

Comments

@chiften1371
Copy link

hi . When I want to create several forms, the theme is only applied to one of them.

@rdbende
Copy link
Owner

rdbende commented Jul 9, 2022

Hi! Can you please clarify what you mean by multi form? And maybe can you attach some example code so I can see where the problem might be?
Without more info it's hard to figure out what the problem is.

@rdbende rdbende added the question Further information is requested label Jul 9, 2022
@chiften1371
Copy link
Author

chiften1371 commented Jul 9, 2022

Thank for your help.
I have two python file. The first file have 1 button that run second python file and open that form.
In first form isn't any problem.
but when i click in button second form open without them (I cant use sv_ttk.use_dark_theme() for my second file) and when I use sv_ttk.use_dark_theme() for my second file error below appear

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\rahbar\PycharmProjects\FoundationInput\main.py", line 40, in t2
    import twoped
  File "C:\Users\rahbar\PycharmProjects\FoundationInput\twoped.py", line 7, in <module>
    sv_ttk.use_dark_theme( )
  File "C:\Users\rahbar\PycharmProjects\FoundationInput\venv\lib\site-packages\sv_ttk\__init__.py", line 59, in <lambda>
    use_dark_theme = lambda: set_theme("dark")
  File "C:\Users\rahbar\PycharmProjects\FoundationInput\venv\lib\site-packages\sv_ttk\__init__.py", line 28, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\rahbar\PycharmProjects\FoundationInput\venv\lib\site-packages\sv_ttk\__init__.py", line 38, in set_theme
    root.tk.call("set_theme", theme)
_tkinter.TclError: can't invoke "winfo" command: application has been destroyed
can't invoke "event" command: application has been destroyed
    while executing
"event generate $w <<ThemeChanged>>"
    (procedure "ttk::ThemeChanged" line 6)
    invoked from within
"ttk::ThemeChanged"

main.py:

import tkinter as tk
from tkinter import ttk
import sv_ttk


MainWindow = tk.Tk()
sv_ttk.use_dark_theme( )
MainWindow.title('Foundation Input')
screen_w = MainWindow.winfo_screenwidth()
screen_h = MainWindow.winfo_screenheight()
form_w = 900
form_h = 600

x = int(screen_w / 2) - int(form_w / 2)
y = int(screen_h / 2) - int(form_h / 2)

MainWindow.geometry('%dx%d+%d+%d' % (form_w, form_h, x, y))
MainWindow.resizable(False, False)
ButtonFrame = ttk.LabelFrame(MainWindow, text="Foundation type", padding=(20, 10))
ButtonFrame.place(x=130, y=130)

LeftVar = tk.IntVar()
RightVar = tk.IntVar()
nextvar = tk.IntVar()


def clear_left():
    LeftVar.set(0)


def clear_right():
    RightVar.set(0)


def t2():
    MainWindow.destroy()
    import twoped


LeftPic = tk.PhotoImage(file='left.png')
RightPic = tk.PhotoImage(file='right.png')

left_pick = ttk.Checkbutton(ButtonFrame, variable=LeftVar, image=LeftPic, style="Toggle.TButton",
                            command=clear_right)
left_pick.grid(column=0, row=0, padx=15, pady=20, sticky="nsew")

left_lable = ttk.Label(ButtonFrame, text="2 Pedstall Foundation", font=('cambria'))
left_lable.grid(column=0, row=1)

right_pick = ttk.Checkbutton(ButtonFrame, variable=RightVar, image=RightPic, style="Toggle.TButton",
                             command=clear_left)
right_pick.grid(column=2, row=0, padx=15, pady=20, sticky="nsew")
right_lable = ttk.Label(ButtonFrame, text="4 Pedstall Foundation", font=('cambria'))
right_lable.grid(column=2, row=1)

next_button = ttk.Button(ButtonFrame, text="Nex Step", style="Accent.TButton",command=t2)
next_button.grid(column=1, row=2, pady=10, padx=10)


MainWindow.mainloop()

twoped.py:

import tkinter as tk
from tkinter import ttk, Tk
import sv_ttk

TwoPedWindow: Tk = tk.Tk( )
sv_ttk.use_dark_theme( )
TwoPedWindow.title('2 Pedstall window')
screen_w = TwoPedWindow.winfo_screenwidth()
screen_h = TwoPedWindow.winfo_screenheight()
form_w = 900
form_h = 600

x = int(screen_w / 2) - int(form_w / 2)
y = int(screen_h / 2) - int(form_h / 2)

TwoPedWindow.geometry('%dx%d+%d+%d' % (form_w, form_h, x, y))
TwoPedWindow.resizable(False, False)
var1 = tk.StringVar( )
var2 = tk.StringVar( )


def s2et():
    a = var2.get( )
    var1.set(a)


b1 = ttk.Button(TwoPedWindow, text="set", command=s2et).pack(side='top')
text1 = ttk.Entry(TwoPedWindow, textvariable=var2).pack(side='top')
Lable1 = ttk.Label(TwoPedWindow, textvariable=var1).pack(side='top')

FoundationInput.zip

@chiften1371
Copy link
Author

@rdbende can you help me?

@rdbende
Copy link
Owner

rdbende commented Jul 11, 2022

This isn't a bug with the theme, but rather a common Tkinter mistake.
The problem is that you use tk.Tk() in your main file, then destroy it, and create a new tk.Tk() in the other file. Though usually this works fine, it's a bad practice, because, tk.Tk() doesn't just create a new window, it creates a full embedded Tcl interpreter that Tkinter, and when you destroy it, it quits that interpreter.
sv_ttk stores the reference of the first tk.Tk instance created, and uses it to make Tcl calls (to set the theme). So if you destroy the main window, but then want to use sv_ttk again, it won't work, since the application has been destroyed.
I suggest you to not destroy the main window and create a new one, but instead use a tk.Toplevel() window.

PS: (I hope this makes any sense)

@rdbende rdbende changed the title don't work in multi form sv_ttk doesn't work with multiple tkinter.Tk instances Jul 11, 2022
@rdbende rdbende added cantfix This cannot be worked on and removed question Further information is requested labels Jul 11, 2022
@rdbende rdbende closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2022
@rdbende rdbende added wontfix and removed cantfix This cannot be worked on labels Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants