返回一个值的对话框然后在不使用调用类中的辅助函数的情况下破坏了self?

时间:2019-01-07 05:23:25

标签: python-3.x tkinter

我的程序涉及根据输入的数据创建对象。如果输入的数据可能不正确但不会破坏程序(例如拼写错误),则会向用户提示对话框,警告错误并询问他们是否要继续。

非常精简的代码:

from tkinter import *
from tkinter.ttk import *

class Tab_ProjectDesigner:

    def __init__(self):    
        self.proceed = BooleanVar(self.page, False, "proceed")

        self.str_messages = StringVar(self.page, name="str_messages")
        #Contains information about possible errors, if any

        ##self.page is Frame containing all widgets in this class##

    def warning(self):
        #creates dialog warning user of possible errors before proceeding

        warningBox = WarningBox(self.page, self, self.str_messages.get())

    def createObject(self):
        #if errors present:
             self.warning()
        #if self.proceed.get() == True:
             #proceed to create object
        return

class WarningBox(Toplevel):
    def __init__(self, parentWidget, parentClass, warningText):
        super().__init__(parentWidget)

        self.proceed = parentClass.proceed

        lbl    = Label (self, text=warningText + "\nAre you sure you want to proceed?")
        btnYes = Button(self, text="Yes", command=self.true)
        btnNo  = Button(self, text="No",  command=self.false)

        return

    def true(self):
        self.proceed.set(True)
        self.destroy()
        return

    def false(self):
        self.proceed.set(False)
        self.destroy()
    return
如果可能出现错误,

createObject()调用 warning() warning()创建 WarningBox ,它会提示用户,并在 Tab_ProjectDesigner 实例中调用的 self.proceed 进行修改。 。 警告框然后自我毁灭。

代码有效,但是让我感到奇怪。有没有一种方法可以使WarningBox处理其自己的行为/事件,然后简单地将一个值返回给创建它的函数?喜欢:

def warning(Self):
    #if erorrs:
        proceed = WarningBox():
    if proceed == True:
        #proceed to object creation

1 个答案:

答案 0 :(得分:0)

一个类不能返回值,它总是返回该类的实例。

您可以 做的是让代码等待对话框窗口关闭。然后,当您执行WarningBox(...).proceed时,首先执行WarningBox(...),仅在关闭窗口后返回,然后在对话框关闭后,读取proceed属性,即{{1 }},TrueFalse,取决于关闭对话框的方式。

None

P.S。我强烈建议您在tkinter和ttk中都不要使用通配符(from tkinter import * from tkinter.ttk import * class Tab_ProjectDesigner: def __init__(self): self.page = Tk() self.str_messages = StringVar(self.page, name="str_messages") self.str_messages.set('test string') self.createObject() self.page.mainloop() def createObject(self): if WarningBox(self.page, self, self.str_messages.get()).proceed: print('Continue') else: print('Stop') class WarningBox(Toplevel): def __init__(self, parentWidget, parentClass, warningText): super().__init__(parentWidget) # Let the toplevel be transient: # A transient window is always drawn on top of its master, # and is automatically hidden when the master is iconified or withdrawn. # Under Windows, transient windows don’t show show up in the task bar. self.transient(parentWidget) # Set default value in case of closing the Toplevel instead of pciking an option self.proceed = None Label (self, text=warningText + "\nAre you sure you want to proceed?").pack() Button(self, text="Yes", command=self.true).pack() Button(self, text="No", command=self.false).pack() # Route all events for this application to this widget self.grab_set() # wait until the Toplevel window is destroyed before returning self.wait_window(self) def true(self): self.proceed = True self.destroy() def false(self): self.proceed = False self.destroy() Tab_ProjectDesigner() )。这些模块中有许多具有相同名称的小部件,这可能会引起您所使用的小部件来自哪个模块的困惑。