0

我有一个程序需要一个对话框来询问存储在global变量中的输入字符串;目前,我正在使用一个Toplevel窗口作为主窗口的子Tk窗口来执行此操作。问题是变量已更改(这有效,我在回调方法中检查过),但是一旦我在窗口上global调用,该值就不会保留。destroyToplevel

from Tkinter import *

class GUI:
    def __init__(self):
        """initialization"""
        # widgets

        self.window = Tk()

        get_string = Button(
            self.window,
            command = self.on_get_string,
            text = "Open string"
            )

        # pack the widgets

        get_string.pack()
        return

    def main(self):
        """the main method"""
        self.window.mainloop()
        return

    def on_get_string(self, event = None):
        """open the string window"""
        prompt = PromptString() # a dialog which prompts for a string

        print str(prompt.string) # None
        prompt.main()
        print str(prompt.string)
        return

class PromptString:
    def __init__(self):
        """initialization"""
        self.string = None # the string

        # widgets

        self.window = Toplevel()

        self.input = Entry(self.window)
        self.input.bind("<Return>", self.on_set_string)

        set_button = Button(
            self.window,
            command = self.on_set_string,
            text = "Set"
            )

        # pack the widgets

        self.input.pack(side = LEFT)
        set_button.pack(side = RIGHT)
        return

    def main(self):
        """the main method"""
        self.window.mainloop() # execution pauses after this line finishes
        return

    def get_input(self):
        """get the input"""
        return self.input.get()

    def on_set_string(self, event = None):
        """set the string variable and close the window"""
        self.string = self.get_input()
        self.window.destroy()
        return

GUI().main()

我无法解决这个问题让我很痛苦,但任何帮助将不胜感激,我提前感谢你。

编辑:

对于造成的混乱,我深表歉意,但这次我实际上重现了该错误,尽管它与其说是错误,不如说是一个问题。问题是程序的执行在第 39 行的调用中停止。我尝试单击并输入 2 个后续值,这些值在实例关闭prompt.main()以相反的顺序打印。这种暂停执行是 Tkinter 的产物吗?我怎样才能解决这个问题?Tk

编辑1:

有问题的变量不是全局的,而是PromptString类的属性。

4

1 回答 1

0

解决方案是使用Tk'wait_window(other_window)方法。这允许执行不会被Tkinter的执行结构中止。固定代码如下:

from Tkinter import *

class GUI:
    def __init__(self):
        """initialization"""
        # widgets

        self.window = Tk()

        get_string = Button(
            self.window,
            command = self.on_get_string,
            text = "Open string"
            )

        # pack the widgets

        get_string.pack()
        return

    def main(self):
        """the main method"""
        self.window.mainloop()
        return

    def on_get_string(self, event = None):
        """open the string window"""
        prompt = PromptString() # a dialog which prompts for a string

        print str(prompt.string) # None
        prompt.wait(self.window) # wait for the prompt
        print str(prompt.string)
        return

class PromptString:
    def __init__(self):
        """initialization"""
        self.string = None # the string

        # widgets

        self.window = Toplevel()

        self.input = Entry(self.window)
        self.input.bind("<Return>", self.on_set_string)

        set_button = Button(
            self.window,
            command = self.on_set_string,
            text = "Set"
            )

        # pack the widgets

        self.input.pack(side = LEFT)
        set_button.pack(side = RIGHT)
        return

    def main(self):
        """the main method"""
        self.window.mainloop() # execution pauses after this line finishes
        return

    def get_input(self):
        """get the input"""
        return self.input.get()

    def on_set_string(self, event = None):
        """set the string variable and close the window"""
        self.string = self.get_input()
        self.window.destroy()
        return

    def wait(self, other_window):
        """run the window as an offshoot of another window"""
        other_window.wait_window(self.window)
        return

GUI().main()
于 2017-06-26T04:31:37.413 回答