所以我在程序中有这些按钮,我希望它们在字典中,而不是在单独的值中,按钮现在都是一样的,但它们会在以后的代码中改变,所以这就是为什么我让它们成为单独的值
answer_1_1 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_1_2 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_1_3 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_1_4 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_1_5 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_2_1 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_2_2 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_2_3 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_2_4 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_2_5 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_3_1 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_3_2 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_3_3 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_3_4 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_3_5 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_4_1 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_4_2 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_4_3 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_4_4 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_4_5 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_5_1 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_5_2 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_5_3 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_5_4 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_5_5 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_6_1 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_6_2 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_6_3 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_6_4 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_6_5 = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
answer_1_1.grid(row = 1, column = 1, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_1_2.grid(row = 1, column = 2, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_1_3.grid(row = 1, column = 3, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_1_4.grid(row = 1, column = 4, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_1_5.grid(row = 1, column = 5, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_2_1.grid(row = 2, column = 1, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_2_2.grid(row = 2, column = 2, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_2_3.grid(row = 2, column = 3, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_2_4.grid(row = 2, column = 4, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_2_5.grid(row = 2, column = 5, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_3_1.grid(row = 3, column = 1, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_3_2.grid(row = 3, column = 2, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_3_3.grid(row = 3, column = 3, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_3_4.grid(row = 3, column = 4, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_3_5.grid(row = 3, column = 5, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_4_1.grid(row = 4, column = 1, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_4_2.grid(row = 4, column = 2, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_4_3.grid(row = 4, column = 3, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_4_4.grid(row = 4, column = 4, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_4_5.grid(row = 4, column = 5, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_5_1.grid(row = 5, column = 1, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_5_2.grid(row = 5, column = 2, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_5_3.grid(row = 5, column = 3, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_5_4.grid(row = 5, column = 4, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_5_5.grid(row = 5, column = 5, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_6_1.grid(row = 6, column = 1, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_6_2.grid(row = 6, column = 2, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_6_3.grid(row = 6, column = 3, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_6_4.grid(row = 6, column = 4, sticky = 'nesw', padx = 2.5, pady = 2.5)
answer_6_5.grid(row = 6, column = 5, sticky = 'nesw', padx = 2.5, pady = 2.5)
我希望他们是这样的:
dict = {'answer1': tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold')),
'answer2': tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold')),
...
}
有没有什么关于如何用for循环来做这件事以及如何访问字典中的按钮的想法?
2条答案
按热度按时间dgiusagp1#
我建议使用以下结构-创建一个嵌套循环,其中外部循环是问题,内部循环是每个问题的答案。对于每个答案,创建一个按钮(
curr_button
)并将其保存在字典中。字典是一个嵌套的字典,其中每个问题都有一个键,它本身就是一个字典,每个答案都有一个键。qzwqbdag2#
给予一个例子,说明如何编写一个高效的问答应用程序。
创建你需要的东西,并使用适当的技术来实现它。
为了自学,这里有一个例子: