Tkinter - 添加标签的按钮
Tkinter - Button to add label
所以我目前正在使用 Tkinter,并且我在尝试创建的程序上已经取得了很大进展。
目前我想要一个在框架中添加标签的按钮。
这是我的代码:
import tkinter as tk
#Window
root = tk.Tk()
root.geometry("800x700")
root.title("Account Overview")
#Title_Frame
title_frame = tk.Frame(root, width=800, height=100, bg='#b1b7ba')
title_frame.pack(side='top')
title_label = tk.Label(title_frame, text="Account Overview", font='Courier 35 underline')
title_label.place(relx=0.2, rely=0.2)
#Account_Frame
account_frame = tk.Frame(root, width=300, height=700, bg='#9dd7f5')
account_frame.pack(side='right')
add_account_button = tk.Button(account_frame, text="Add Account", font='Courier 15')
add_account_button.place(relx=0.26, rely=0.03)
account = tk.Label(account_frame, text="Account Nr1", font='Helvetica 40')
account.place(relx=0.01, rely=0.125)
root.mainloop()
现在,如果您尝试代码,您会看到在打开的 window 中,有一个 侧框
它会显示一个按钮,上面写着 “添加帐户”。在它下面,您可以看到一个标签,上面写着 "Account Nr1"。我想要发生的是,一旦你打开程序,"Account Nr1" 标签就不会出现,它只会在你点击后出现在那个确切的位置“添加帐户”。
根据您的代码,一个快速的解决方案是在屏幕外创建标签,然后在单击按钮时将其移动。
def showlabel():
account.place(relx=0.01, rely=0.125) # move on screen
#Account_Frame
account_frame = tk.Frame(root, width=300, height=700, bg='#9dd7f5')
account_frame.pack(side='right')
add_account_button = tk.Button(account_frame, text="Add Account", font='Courier 15', command=showlabel)
add_account_button.place(relx=0.26, rely=0.03)
account = tk.Label(account_frame, text="Account Nr1", font='Helvetica 40')
account.place(relx=10.01, rely=0.125) # off screen
root.mainloop()
如果您使用网格设置表单,则可以使用 forget
方法隐藏标签:https://www.geeksforgeeks.org/python-forget_pack-and-forget_grid-method-in-tkinter/
---更新---
要动态创建标签,请创建标签列表并使用按钮创建新标签并附加到列表中。
lbllst = []
def addlabel():
account = tk.Label(account_frame, text="Account Nr"+str(len(lbllst)+1), font='Helvetica 40')
account.place(relx=0.01, rely=0.125*(len(lbllst)+1))
lbllst.append(account)
#Account_Frame
account_frame = tk.Frame(root, width=300, height=700, bg='#9dd7f5')
account_frame.pack(side='right')
add_account_button = tk.Button(account_frame, text="Add Account", font='Courier 15', command=addlabel)
add_account_button.place(relx=0.26, rely=0.03)
root.mainloop()
所以我目前正在使用 Tkinter,并且我在尝试创建的程序上已经取得了很大进展。 目前我想要一个在框架中添加标签的按钮。
这是我的代码:
import tkinter as tk
#Window
root = tk.Tk()
root.geometry("800x700")
root.title("Account Overview")
#Title_Frame
title_frame = tk.Frame(root, width=800, height=100, bg='#b1b7ba')
title_frame.pack(side='top')
title_label = tk.Label(title_frame, text="Account Overview", font='Courier 35 underline')
title_label.place(relx=0.2, rely=0.2)
#Account_Frame
account_frame = tk.Frame(root, width=300, height=700, bg='#9dd7f5')
account_frame.pack(side='right')
add_account_button = tk.Button(account_frame, text="Add Account", font='Courier 15')
add_account_button.place(relx=0.26, rely=0.03)
account = tk.Label(account_frame, text="Account Nr1", font='Helvetica 40')
account.place(relx=0.01, rely=0.125)
root.mainloop()
现在,如果您尝试代码,您会看到在打开的 window 中,有一个 侧框 它会显示一个按钮,上面写着 “添加帐户”。在它下面,您可以看到一个标签,上面写着 "Account Nr1"。我想要发生的是,一旦你打开程序,"Account Nr1" 标签就不会出现,它只会在你点击后出现在那个确切的位置“添加帐户”。
根据您的代码,一个快速的解决方案是在屏幕外创建标签,然后在单击按钮时将其移动。
def showlabel():
account.place(relx=0.01, rely=0.125) # move on screen
#Account_Frame
account_frame = tk.Frame(root, width=300, height=700, bg='#9dd7f5')
account_frame.pack(side='right')
add_account_button = tk.Button(account_frame, text="Add Account", font='Courier 15', command=showlabel)
add_account_button.place(relx=0.26, rely=0.03)
account = tk.Label(account_frame, text="Account Nr1", font='Helvetica 40')
account.place(relx=10.01, rely=0.125) # off screen
root.mainloop()
如果您使用网格设置表单,则可以使用 forget
方法隐藏标签:https://www.geeksforgeeks.org/python-forget_pack-and-forget_grid-method-in-tkinter/
---更新---
要动态创建标签,请创建标签列表并使用按钮创建新标签并附加到列表中。
lbllst = []
def addlabel():
account = tk.Label(account_frame, text="Account Nr"+str(len(lbllst)+1), font='Helvetica 40')
account.place(relx=0.01, rely=0.125*(len(lbllst)+1))
lbllst.append(account)
#Account_Frame
account_frame = tk.Frame(root, width=300, height=700, bg='#9dd7f5')
account_frame.pack(side='right')
add_account_button = tk.Button(account_frame, text="Add Account", font='Courier 15', command=addlabel)
add_account_button.place(relx=0.26, rely=0.03)
root.mainloop()