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

tv6aics1  于 2023-05-30  发布在  Python
关注(0)|答案(2)|浏览(159)

所以我在程序中有这些按钮,我希望它们在字典中,而不是在单独的值中,按钮现在都是一样的,但它们会在以后的代码中改变,所以这就是为什么我让它们成为单独的值

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循环来做这件事以及如何访问字典中的按钮的想法?

dgiusagp

dgiusagp1#

我建议使用以下结构-创建一个嵌套循环,其中外部循环是问题,内部循环是每个问题的答案。对于每个答案,创建一个按钮(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
qzwqbdag

qzwqbdag2#

给予一个例子,说明如何编写一个高效的问答应用程序。
创建你需要的东西,并使用适当的技术来实现它。
为了自学,这里有一个例子:

import tkinter as tk

q_and_a = {
    'How old are you?' : [
        16, #0
        18, #1
        21, #2
        48],#3
    'Where do you live?' : [
        'USA',
        'India',
        'England',
        'Syria'],
    }

right_answer = {
    'How old are you?' : 0,
    'Where do you live?' : 2,
    }

def generate_q_and_a():
    for k,v in q_and_a.items():
        yield k,v
generator = generate_q_and_a()

def next_question():
    try:
        q, a = next(generator)
    except StopIteration:
        print('all questions have solved')
        root.destroy()
    else:
        question.config(text = q)
        for idx,child in enumerate(answer_frame.winfo_children()):
            child.config(text=a[idx])

def answer_selected(n):
    current_question = question.cget('text')
    valid = n == right_answer[current_question]
    if valid:
        next_question()
    else:
        print('wrong answer')

BACKGROUND = 'grey'
FOREGROUND = 'black'

root = tk.Tk()
#content of root
question = tk.Label(
    root, text='', bg=BACKGROUND, fg=FOREGROUND, relief=tk.RIDGE)
answer_frame = tk.Frame(
    root, bg = BACKGROUND, borderwidth=2, relief=tk.SUNKEN)
#geometry of root
question.pack(fill=tk.X, expand=True)
answer_frame.pack(fill=tk.BOTH, expand=True)
#content of answer_frame
choice_config = {
    'master' : answer_frame,
    'text' : '',
    'relief' : tk.RAISED,
    'borderwidth' : 1,
    'background' : 'grey',
    'foreground' : 'white',
    }
for i in range(4):
    callback = lambda n=i: answer_selected(n)
    choice = tk.Button(**choice_config, command=callback)
    choice.pack(fill=tk.X, expand=True)
#start application
next_question()
root.mainloop()

相关问题