python - 如何再次在 tkinter 中打开初始窗口?
问题描述
我有以下问题。我想在 tkinter 中创建一个按钮,该按钮将删除现有更改,并且窗口看起来像初始窗口。这是我的初始窗口 1:
这是我单击前两个按钮 Window 2 时窗口的样子:
现在我想点击“Zpět”按钮,我想再次看到 Window 1。这是我的代码:
import tkinter as tk
root = tk.Tk()
home_frame = tk.Frame(root)
home_frame.grid(row=0, column=0, sticky="news")
def raise_new_payment():
tk.Label(text=f"Stav bilance k 2021-09-09").grid()
def back():
"""I would like to this function to clean everything."""
tk.Label().destroy()
platba = tk.Button(
home_frame,
text="Zadej novou platbu",
command=lambda: raise_new_payment(),
)
platba.pack(pady=10)
zpet = tk.Button(
home_frame,
text="Zpět",
command=back,
)
zpet.pack(pady=10)
我不知道如何使用该back()
功能。我试图删除在 中创建的 tk.Label raise_new_payment()
,但它不起作用。你能帮我吗?非常感谢。
解决方案
我建议你创建标签一次,不要.pack()
先调用它,即它最初是不可见的。
然后在里面更新它raise_new_payment()
并调用.pack()
显示它。
你可以再打电话.pack_forget()
把它藏在里面back()
。
import tkinter as tk
root = tk.Tk()
home_frame = tk.Frame(root)
home_frame.grid(row=0, column=0, sticky="news")
def raise_new_payment():
# update label and show it
lbl.config(text=f"Stav bilance k 2021-09-09")
lbl.pack()
def back():
# hide the label
lbl.pack_forget()
platba = tk.Button(
home_frame,
text="Zadej novou platbu",
command=lambda: raise_new_payment(),
)
platba.pack(pady=10)
zpet = tk.Button(
home_frame,
text="Zpět",
command=back,
)
zpet.pack(pady=10)
# create the label and initially hide it
lbl = tk.Label(home_frame)
root.mainloop()
推荐阅读
- regex - 正则表达式从一个字符开始获取两个字符之间的所有内容
- python - 为什么从同一目录中的模块导入模块时必须在模块前加一个点?
- php - 我希望在修改后的 php-mailer 中插入回复按钮
- python - 小写字母出现错误
- java - 如何在对象类的静态方法中使用 return 语句?
- facebook - 该站点如何仍在使用 Facebook 事件 API?
- firebase - Firebase 身份验证身份未在安全 Firestore 规则级别更新
- tensorflow - ResourceExhaustedError 运行网络演示时第四次尝试
- ruby - 从 Ruby 中编译的 protobuffer 消息中获取枚举值
- javascript - 将 google map api(javascript) 与 django 查询集一起使用,我无法很好地访问查询集的查询