tkinter更新按钮列表,具体取决于单击另一个按钮列表
我是TKINTER的新手。我有一个按钮A列表,当我选择一个按钮时,我也可以更新标签(请参阅代码)。现在,我想拥有一个按钮B的第二个列表,其中按钮(和功能)的数量取决于按钮a列表中的单击按钮(我试图在图片中显示此功能)。
我在这里做错了什么?你能帮我吗!
import tkinter as tk
root = tk.Tk()
root.title("Analyser")
root.geometry("750x250")
group_number = tk.StringVar()
l0 = tk.Label(root, textvariable=group_number, width=20)
l0.grid(row=1,column=1)
def func(n):
group_number.set("Selected Button: Button " + str(n+1))
Buttons_A = ["Button 1","Button 2"]
l2 = tk.Label(root, text = "BUTTONS A",font=('Helvetica', 12, "bold"), fg='white', bg='#107baf', width=20)
l2.grid(row=0, column=0,padx=4,pady=8)
for i in range(len(Buttons_A)):
btn=tk.Button(root, text=Buttons_A[i], font=('Helvetica', 10), bg='#d8e7ea', width=20, activebackground='#42bed8')
btn.config(command=lambda n=i:func(n))
btn.grid(row=i+1,column=0, padx=4,pady=4)
Buttons_B_1 = ["Button 1","Button 2","Button 3"]
Buttons_B_2 = ["Button 1"]
l2 = tk.Label(root, text = "BUTTONS B",font=('Helvetica', 12, "bold"), fg='white', bg='#107baf', width=20)
l2.grid(row=0, column=2,padx=4,pady=8)
for i in range(len(Buttons_B_1)):
btn=tk.Button(root, text=Buttons_B_1[i], font=('Helvetica', 10), bg='#d8e7ea', width=20, activebackground='#42bed8')
btn.config(command=lambda n=i:func(n))
btn.grid(row=i+1,column=2, padx=4,pady=4)
root.mainloop()
从按钮列表中选择按钮1时:
当按钮2从按钮列表中选择按钮2:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将按钮B保留在列表中,以便可以轻松销毁它们。
当您单击按钮a时,它将运行
func
,在此功能中,您应该从列表中销毁按钮,清除此列表,创建新按钮并将它们添加到此列表中。如果您将按钮b1和b2作为赞美表在一个列表中,则代码将更简单。
最小工作代码。
我删除了字体和颜色,仅显示重要的元素。
最终,您可以将
frame
用于分组按钮,然后仅破坏此frame
,并且它将破坏按钮。但是,您也可以首先使用按钮创建帧,然后将按钮放入框架中,然后使用
grid_forget()
删除一个帧和grid()
以显示另一个帧。You should keep buttons B on list so you could easily destroy them.
When you click button A then it runs
func
and inside this function you should destroy buttons from list, clear this list, create new buttons and add them to this list.Code will be simpler if you will keep buttons B1 and B2 as sublists on one list.
Minimal working code.
I removed fonts and colors to show only important elements.
Eventually you could use
Frame
to group buttons and then you have to destroy only thisFrame
and it will destroy also buttons.But you can also first create Frames with buttons and put buttons in frames and later use
grid_forget()
to remove one frame andgrid()
to show another frame.您的应用程序中的两列按钮 - 按钮中的a 列表和按钮b 列表中的两个列 - 可以将其组织为两个级别的层次结构,并在这样的字典:
以上格式的数据可用于推动GUI其余部分的构建。由于需要有时能够将按钮b 列按钮作为单个复合单元,例如显示和隐藏它们时,将它们作为单独的“容器”
tk.frame < /code>将变得更容易,因为它们都可以通过仅更改 it 而不是与组个体中的每个小部件进行处理。
我的意思是:
这是一个屏幕截图,显示了最初显示的内容,然后再显示两个单击相应组按钮时发生的情况:
//i.sstatic.net/suaee.png“ rel =“ nofollow noreferrer”>
The two columns of buttons in your application — those in the BUTTON A list and those in the BUTTON B list — can be organized into a two level hierarchy and easily be represented in a dictionary like this:
Data in the above format can be used to drive the construction of the rest of the GUI. Since it's desirable to sometimes be able treat the BUTTON B column buttons as a single compound unit, such as when showing and hiding them, nesting them inside as a separate "container"
tk.Frame
will make that easier because they can all be affected by merely changing it instead of dealing with each widget in the group individual.Here's what I mean:
Here's a screenshot showing what's initially displayed, followed by two more showing what happens when corresponding group button is clicked:
使用 dictionary 为
buttons_a
而不是分离列表,更容易将所需列表关联到所需的列表:也可以更好地将框架使用到组按钮,以便您可以在“按钮b中” butso “然后在单击“按钮”中的按钮时创建新的。
It is easier to associate the required lists using dictionary for
Buttons_A
instead of separated lists:Also better use frames to group buttons, so that you can just destroy buttons in "BUTTONS B" and then create new ones whenever a button in "BUTTONS A" is clicked.