开发者

How to make an arbirtary number of windows using Python and Tkinter?

开发者 https://www.devze.com 2023-02-25 04:27 出处:网络
I\'m writing an app that doesn\'t have a main window (it runs inside a Python interpreter in another app), and I thought I had a good solution for getting Tkinter to cooperate--I made the Tkinter.Tk c

I'm writing an app that doesn't have a main window (it runs inside a Python interpreter in another app), and I thought I had a good solution for getting Tkinter to cooperate--I made the Tkinter.Tk class into a Borg.


class RootWindow(Tk):
    """ Invisible window that serves as the default parent
     开发者_高级运维   of all others.
    """

    groupDict = {}

    def __init__(self):
        self.__dict__ = self.groupDict
        if self.__dict__ == {}: # first instance
            Tk.__init__(self)
            self.withdraw()


Then I have my Windows, which are subclassed from Tkinter.Toplevel, default to parent=RootWindow(). The result should be that I can create any number of Windows and they'll use the same root.

It works once fine for the first Window, but after that things get all messed up. :(

see pic

What am I doing wrong? Is this even a feasible solution?

Thanks

EDIT: I should add that even though there's other stuff running in the picture, the problem can be duplicated just by using RootWindow as the parent of a Tkinter.Toplevel.

EDIT: I overrode Window.mainloop so everything uses the RootWindow event loop.


def mainloop(self):
    self.master.wait_window(self)

Then I create each visible window like this:

test = Window()
test.mainloop()

It seems to work because the windows do show up, but their contents are packed in an odd way that's hard to describe. It alternates between no contents at all and having everything squished horizontally and expanded vertically.


One problem appears to be that you are forgetting to start the event loop. Or, based on further edits of your question, you may be starting more than one main loop. You need exactly one main loop that is run from the root window.

Tkinter certainly allows an arbitrary number of top level windows, but your question doesn't appear to have enough details to show what is wrong unless my first guess is correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消