首页 > 解决方案 > 使用下拉值更新另一个框架

问题描述

我希望通过我选择的下拉值更新我的第二帧。从字面上看,我的下拉列表中的值是字典。我希望每个键的所有值都进入第二帧中各自的位置。

这是我的代码:

from tkinter import *
from tkinter import  ttk # necessary for Notebook
from tkinter import filedialog
import pandas as pd
import numpy as np

######################## GUI SETUP ###################

root =Tk()
root.title('My GUI')
root.geometry('800x600')

# Instantiating the notebook

nbook=ttk.Notebook(root)
nbook.pack(pady='25')


frame_1=Frame(nbook, width=800, height=600, bg='grey')
frame_2=Frame(nbook, width=800, height=600, bg='white')

# Frame packing
frame_1.pack(fill='both', expand=1)
frame_2.pack(fill='both', expand=1)

# Find a way to pack themall together elegeantly
nbook.add(frame_1, text="Setup")
nbook.add(frame_2, text="Overview")

### Dropdown #####
lista=['Pod1','Pod2','Pod3','Pod4']

click=StringVar()
click.set('Select Options')

options=OptionMenu(frame_1, click, *lista)
options.pack()

#### Overview tab ######
Label_1=Label(frame_2, text='Main')
Label_1.place(x=70, y=30)

Label_2=Label(frame_2, text='Second')
Label_2.place(x=350, y=30)

Label_3=Label(frame_2, text='Others')
Label_3.place(x=600,y=30)

pod1={'Main':['Strawberries','Blueberries','Raspberries'],'Second':    ['Blackberries', 'Burberries'], 'Rest':None}
pod2={'Main':['Mangoes', 'Guavas'],'Second':['Coconut'],'Rest': None}
pod3={'Main':['Tiger', 'Lions'],'Second':['Leopard','Cheetah'], ,'Rest': ['Cat', 'Lynx','C']}

root.mainloop() # this must end the program

标签: pythontkinter

解决方案


这是否让您对如何进行有所了解,我只是很快就完成了,它应该给您一些想法:D

将您的字典更改为:

pod1={'Main':['Strawberries','Blueberries','Raspberries'],'Second':['Blackberries', 'Burberries'], 'Rest':['None']}
pod2={'Main':['Mangoes', 'Guavas'],'Second':['Coconut'],'Rest': ['None']}
pod3={'Main':['Tiger', 'Lions'],'Second':['Leopard','Cheetah'], 'Rest': ['Cat', 'Lynx','C']}

制作一个按钮并像它一样发挥作用

b = Button(frame_1,text='Click me!',command=hmm)
b.pack(pady=20)

定义函数

def hmm():
    global la
    if click.get() == 'Pod1':
        for index,l in enumerate(pod1['Main']):
            la = Label(frame_2,text='')
            la.place(x=70,y=index*25+50)
            la.config(text=l)
        for index1,l1 in enumerate(pod1['Second']):
            la = Label(frame_2,text='')
            la.place(x=350,y=index1*25+50)
            la.config(text=l1)
        for index2,l2 in enumerate(pod1['Rest']):
            la = Label(frame_2,text='')
            la.place(x=600,y=index2*25+50)
            la.config(text=l2)

    elif click.get() == 'Pod2':
        for index,l in enumerate(pod2['Main']):
            la = Label(frame_2,text='')
            la.place(x=70,y=index*25+50)
            la.config(text=l)
        for index1,l1 in enumerate(pod2['Second']):
            la = Label(frame_2,text='')
            la.place(x=350,y=index1*25+50)
            la.config(text=l1)
        for index2,l2 in enumerate(pod2['Rest']):
            la = Label(frame_2,text='')
            la.place(x=600,y=index2*25+50)
            la.config(text=l2)
    
    elif click.get() == 'Pod3':
        for index,l in enumerate(pod3['Main']):
            la = Label(frame_2,text='')
            la.place(x=70,y=index*25+50)
            la.config(text=l)
        for index1,l1 in enumerate(pod3['Second']):
            la = Label(frame_2,text='')
            la.place(x=350,y=index1*25+50)
            la.config(text=l1)
        for index2,l2 in enumerate(pod3['Rest']):
            la = Label(frame_2,text='')
            la.place(x=600,y=index2*25+50)
            la.config(text=l2)

让我知道是否有任何不清楚的地方,这只是我的想法,你完全可以以不同的方式进行,也可能得到更好的输出。干杯


推荐阅读