【问题标题】:Automatically close tk ENTRY window after clicking submit button点击提交按钮后自动关闭 tk ENTRY 窗口
【发布时间】:2017-04-18 00:44:54
【问题描述】:

我正在 python 3.5 中创建一个注销程序。我一直在环顾四周,看到了如何使用“关闭/退出/退出”按钮关闭窗口。我想要做的是在他们单击“提交”按钮以将输入框中的文本发送到文件后自动关闭窗口。我试图在 write_to_file 中以不同的方式添加 .destroy(),但我一直收到一个错误,说它没有定义。我为它创建了一个定义,就像我见过的一些例子一样,但我不知道如何让 write_to_file 调用它。程序将条目框中的内容正确写入文件。任何帮助将不胜感激。

class Writefiles:

    def __init__(self):
        win3 = Tk()
        win3.title('Signature')
        win3['bg'] = 'blue'
        win3.geometry('300x200')
        center(win3)

        self.VarEnt = StringVar()

        self.lab = Label(win3, text = "Name")
        self.lab.grid(padx = 10, pady = 10)

        self.ent = Entry(win3, textvariable = self.VarEnt, bd = 5, width = 45)
        self.ent.focus()
        self.ent.grid(padx = 10, pady = 10)

        self.btn = Button(win3, text = 'Submit', width = 10, height = 2, background = 'gold', command = self.write_to_file)
        self.btn.grid(padx = 10, pady = 10)


    def write_to_file(self):

        date = datetime.now().strftime('   %Y-%m-%d  %H:%M:%S') 

        with open('sig.txt', 'a') as f:
            f.write(self.ent.get() + date + '\n')
            f.close()


    def close_win(self):        # close tkinter window
        self.ent.destroy()

【问题讨论】:

  • 关闭窗口确实是通过在该窗口上调用destroy() 来完成的。如果您需要任何错误的帮助,您将不得不发布这些错误。

标签: tkinter python-3.5


【解决方案1】:

你必须破坏win3窗口而不是入口。写吧 win3.destroy() 在 def write_to_file(self) 的末尾。

您可以销毁窗口(如根窗口或作为根窗口子级的顶层窗口)而不是窗口的小部件。销毁根窗口会销毁所有窗口并退出程序。

在 def close_win(self) 中,您试图销毁 ent 条目而不是其父窗口 win3

【讨论】:

  • 当我将它添加到 def 的末尾时,我得到 NameError: 'win3' is not defined。我是否需要向它添加一些内容以便它会在 init 部分中查找它?我尝试添加自我,但随后出现 AttributeError: 'Writefiles' object has no attribute 'win3'。
  • 抱歉耽搁了。我没有看到问题...您使用 win3=Tk() 并将其称为 win3。您必须在类的定义中使用 self.win3。所以把类里面的所有win3都改成self.win3。另外我不确定你是否需要全局win3
  • 它可以双向工作,但我现在明白使用'self'是python方式,所以我将它添加到类中的所有win3并删除了全局。谢谢
  • 没有 self 就无法访问变量。在类的其他方法或类外。如果类中的变量中没有 self ,则您有一个局部变量。它就像函数的局部变量:只有将其设为全局变量,才能在函数外部访问它。这就是为什么你将 self 设置在类中的变量前面。
【解决方案2】:

我需要将 win3 设为全局,然后当我将 win3.destroy() 添加到 write_to_file 时,它​​会在单击按钮后关闭窗口。如果你不让它成为全局的,你会得到一个 NameError: 'win3' is not defined。

我删除了 def close_win(self): 部分。

更正后的代码:

class Writefiles:

def __init__(self):
    self.win3 = Tk()
    self.win3.title('Signature')
    self.win3['bg'] = 'blue'
    self.win3.geometry('300x200')
    center(self.win3)

    self.VarEnt = StringVar()

    self.lab = Label(self.win3, text = "Name")
    self.lab.grid(padx = 10, pady = 10)

    self.ent = Entry(self.win3, textvariable = self.VarEnt, bd = 5, width = 45)
    self.ent.focus()
    self.ent.grid(padx = 10, pady = 10)

    self.btn = Button(self.win3, text = 'Submit', width = 10, height = 2, background = 'gold', command = self.write_to_file)
    self.btn.grid(padx = 10, pady = 10)


def write_to_file(self):

    date = datetime.now().strftime('   %Y-%m-%d  %H:%M:%S')

    with open('sig.txt', 'a') as f:
        f.write(self.ent.get() + date + '\n')
        f.close()

    self.win3.destroy()   # to close window after files is written

编辑 在看到@gms 评论后,我删除了global 并将self. 添加到win3 条目中。我编辑了上面的代码以显示更正。

【讨论】:

    猜你喜欢
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多