【问题标题】:Destroy GUI window in python tkinter在 python tkinter 中销毁 GUI 窗口
【发布时间】:2021-03-02 15:19:01
【问题描述】:

我是python开发的新手,从this链接开始学习多客户端、服务器开发。我想通过添加一个关闭按钮而不是窗口提供的关闭按钮来调整这个应用程序。

# GUI class for the chat 
class GUI:
    # constructor method
  def __init__(self):

    # chat window which is currently hidden
    self.Window = Tk()
    self.Window.withdraw()

    # login window
    self.login = Toplevel()
    # set the title
    self.login.title("Login")
    self.login.resizable(width=False,
                         height=False)
    self.login.configure(width=400,
                         height=300)
    # create a Label
    self.pls = Label(self.login,
                     text="Please login to continue",
                     justify=CENTER,
                     font="Helvetica 14 bold")

    self.pls.place(relheight=0.15,
                   relx=0.2,
                   rely=0.07)
    # create a Label
    self.labelName = Label(self.login,
                           text="Name: ",
                           font="Helvetica 12")

    self.labelName.place(relheight=0.2,
                         relx=0.1,
                         rely=0.2)

    # create a entry box for
    # tyoing the message
    self.entryName = Entry(self.login,
                           font="Helvetica 14")

    self.entryName.place(relwidth=0.4,
                         relheight=0.12,
                         relx=0.35,
                         rely=0.2)

    # set the focus of the curser
    self.entryName.focus()

    self.name = self.entryName.get()
    # create a Continue Button
    # along with action
    self.go = Button(self.login,
                     text="CONTINUE",
                     font="Helvetica 14 bold",
                     command=lambda: self.goAhead(self.entryName.get()))

    self.go.place(relx=0.4,
                  rely=0.55)

    self.Window.mainloop()

def goAhead(self, name):
    self.login.destroy()
    self.layout("Client chat")


 # The main layout of the chat
def layout(self, name):

    self.name = name
    # to show chat window
    self.Window.deiconify()
    self.Window.title(self.name)
    self.Window.resizable(width=False,
                          height=False)
    self.Window.configure(width=470,
                          height=550,
                          bg="#17202A")
    self.labelHead = Label(self.Window,
                           bg="#17202A",
                           fg="#EAECEE",
                           text=self.name,
                           font="Helvetica 13 bold",
                           pady=5)

    self.labelHead.place(relwidth=1)
    self.line = Label(self.Window,
                      width=450,
                      bg="#ABB2B9")

    self.line.place(relwidth=1,
                    rely=0.07,
                    relheight=0.012)

    self.textCons = Text(self.Window,
                         width=20,
                         height=2,
                         bg="#17202A",
                         fg="#EAECEE",
                         font="Helvetica 14",
                         padx=5,
                         pady=5)

    self.textCons.place(relheight=0.745,
                        relwidth=1,
                        rely=0.08)

    self.labelBottom = Label(self.Window,
                             bg="#ABB2B9",
                             height=80)

    self.labelBottom.place(relwidth=1,
                           rely=0.825)
    # create a Send Button
    self.buttonMsg = Button(self.labelBottom,
                            text="Close",
                            font="Helvetica 10 bold",
                            width=20,
                            bg="#ABB2B9",
                            command=lambda: self.closeConnection())

    self.buttonMsg.place(relx=0.50,
                         rely=0.008,
                         relheight=0.06,
                         relwidth=0.22)

    self.selectbuttonMsg = Button(self.labelBottom,
                                  text="Select",
                                  font="Helvetica 10 bold",
                                  width=20,
                                  bg="#ABB2B9",
                                  command=lambda: self.readFile())

    self.selectbuttonMsg.place(relx=0.25,
                               rely=0.008,
                               relheight=0.06,
                               relwidth=0.22)

    self.textCons.config(cursor="arrow")

    # create a scroll bar
    scrollbar = Scrollbar(self.textCons)

    # place the scroll bar
    # into the gui window
    scrollbar.place(relheight=1,
                    relx=0.974)

    scrollbar.config(command=self.textCons.yview)

    self.textCons.config(state=DISABLED)

我添加了按钮并添加了名为 closeConnection 的函数。我使用了所有可能的东西,UI 卡住了,但窗口没有关闭。

    def closeConnection(self):
        quit()
        self.Window.quit()
        self.Window.destroy()

我试图弄清楚但无法理解我错过了什么。是由于线程还是由于窗口去图标化的东西,还是我不了解窗口层次结构。

提前致谢。

【问题讨论】:

  • 第一个问题是你的缩进关闭了。确保适当地间隔不同的级别。那就是说你的代码对我来说有适当的缩进。我通常只是root.destroy() 或在你的情况下self.Window.destroy() 在主循环之后没有其他代码要执行,它应该关闭所有内容。我可能是错的,因为我通常使对象继承自 Tk() 并使用 self.destroy() 但据我了解,您的代码应该很好地退出并关闭所有内容,因为一旦主循环退出,就没有其他代码可以运行了。
  • 因为它是一个客户端服务器应用程序,你认为我们需要在另一个线程上运行 root.destroy 吗?
  • Tkinter 不是线程保存。这意味着它需要在主线程中运行才能正常运行。如果您尝试在另一个线程中运行 Tkinter,这可能是问题的一部分。

标签: python python-3.x multithreading sockets tkinter


【解决方案1】:

我认为您必须使用此命令才能关闭窗口:self.root.destroy()

【讨论】:

  • OPs 代码中没有名为root 的属性。 self.Window.destroy() 是正确的,应该可以自己正常工作。
猜你喜欢
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多