python - 如何使用 Python 将多个标签变量转换为 tkinter 中的字典?

标签 python python-3.x dictionary tkinter

所以我的程序中有这些按钮,我希望它们位于字典中,而不是位于单独的值中, 按钮现在都是相同的,但它们稍后会在代码中更改,这就是为什么我将它们设为单独的值

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 循环执行此操作以及如何访问字典中的按钮有什么想法吗?

最佳答案

我建议采用以下结构 - 创建一个嵌套循环,其中外部循环是问题,内部循环是每个问题的答案。对于每个答案,创建一个按钮 (curr_button) 并将其保存在字典中。该字典是一个嵌套字典,其中每个问题都有一个键,该字典本身也是一个字典,其中每个答案都有键。

from itertools import product 
from collections import defaultdict

num_questions = 6
answers_per_question = 4

buttons_dict = defaultdict(dict)

for q in range(1, num_questions+1):
    for a in range(1, answers_per_question+1):
        curr_button = tk.Label(base_frame, text='', fg='black', bg='white', highlightbackground='#d3d6da', highlightthickness=2, font = ('Helvetica', 32, 'bold'))
        curr_button.grid(row = q, column = a, sticky = 'nesw', padx = 2.5, pady = 2.5)
        buttons_dict[q][a]=curr_button
        

关于python - 如何使用 Python 将多个标签变量转换为 tkinter 中的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76350205/

相关文章:

python - 无法在 MAC OS 中通过 pip 安装 rpy2

python - 将大约 100 个单选按钮分组

python - 'map' 类型的对象在 Python 3 中没有 len()

linux - 无法在 AWS Sagemaker 上安装 spacy

arrays - VBA Excel 计算特定值

ios - 从 Swift 3 中的字典中删除重复值

python - pandas 根据另一列获取 True 值的(开始、结束)位置

python - 如何在caffe模型中获取概率

Python discord.py 安装

java - 在 Java 8 中从 Stream 惯用地创建多值 Map