python tkinter 点击关闭时隐藏窗口
时间: 2023-07-12 18:16:52 浏览: 65
你可以使用 `protocol` 方法来绑定窗口关闭事件,并使用 `withdraw` 方法隐藏窗口。下面是一个简单的示例代码:
```python
import tkinter as tk
root = tk.Tk()
def on_closing():
root.withdraw()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
```
在上面的代码中,当用户点击窗口上的关闭按钮时,`on_closing` 函数会被调用,该函数会使用 `withdraw` 方法隐藏窗口。注意,使用 `withdraw` 方法隐藏窗口后,你需要使用其他方法来重新显示窗口。
相关问题
python tkinter 点击关闭时不关闭窗口,隐藏窗口
你可以使用 `protocol` 方法来绑定窗口关闭事件,并使用 `withdraw` 方法隐藏窗口,以达到点击关闭时不关闭窗口的效果。下面是一个简单的示例代码:
```python
import tkinter as tk
root = tk.Tk()
def on_closing():
root.withdraw()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
```
在上面的代码中,当用户点击窗口上的关闭按钮时,`on_closing` 函数会被调用,该函数会使用 `withdraw` 方法隐藏窗口。注意,使用 `withdraw` 方法隐藏窗口后,你需要使用其他方法来重新显示窗口。
python中tkinter隐藏主窗口代码
以下是Python中tkinter隐藏主窗口的代码:
```python
import tkinter as tk
# 创建主窗口并隐藏
root = tk.Tk()
root.withdraw()
# 显示消息框
tk.messagebox.showinfo("Title", "Message")
# 关闭主窗口
root.quit()
```