Pregunta

I want to display the same label (textvariable) on multiple windows at once but it will only show the content on the window that is created first. The label appears on the other but it is basically an empty field.

Something interesting to note is that if I replace textvariable with text (and other necessary changes) It will display the static text as expected.

example code below:

from tkinter import *

root = Tk()
root.overrideredirect(1)

root2 = Tk()
root2.protocol("WM_DELETE_WINDOW", exit)

class SubsLight(Frame):
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self.T1Subs = 'A'
        self.T2Subs = 'B'
        self.T1Subsstr = StringVar()
        self.T2Subsstr = StringVar()
        self.Subset()

    def Subset(self):
        self.T1Subsstr.set(self.T1Subs)
        self.T2Subsstr.set(self.T2Subs)
        T1Sub = Label(root, textvariable=self.T1Subsstr, font=("Arial", 20), fg='#fff188188', bg='black', width=1, padx=5, pady=5)
        T2Sub = Label(root, textvariable=self.T2Subsstr, font=("Arial", 20), fg='#fff188188', bg='black', width=1, padx=5, pady=5)
        T1Sub.grid()
        T2Sub.grid()

        T12Sub = Label(root2, textvariable=self.T1Subsstr, font=("Arial", 20), fg='#fff188188', bg='black', width=1, padx=5, pady=5)
        T12Sub.grid()
        T12Sub = Label(root2, textvariable=self.T2Subsstr, font=("Arial", 20), fg='#fff188188', bg='black', width=1, padx=5, pady=5)
        T12Sub.grid()

sub = SubsLight(root)

def main():
    sub.grid()

    root.mainloop()

if __name__ == '__main__':
    main()

I can get the label to display in the other window by moving root2 to above root but then I have the opposite problem.

first of all why doesn't it work as it is, and how do I get it to work as I described.

I realize having two windows isn't a common thing, but it is necessary for this project as one is the main display and the other is the controls.

¿Fue útil?

Solución

The problem is that you have two instances of Tk. Tkinter is designed to work with exactly one instance of that class. If you want more than one window, create instances of Toplevel

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top