python - 使用下拉值更新另一个框架
问题描述
我希望通过我选择的下拉值更新我的第二帧。从字面上看,我的下拉列表中的值是字典。我希望每个键的所有值都进入第二帧中各自的位置。
这是我的代码:
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
解决方案
这是否让您对如何进行有所了解,我只是很快就完成了,它应该给您一些想法: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)
让我知道是否有任何不清楚的地方,这只是我的想法,你完全可以以不同的方式进行,也可能得到更好的输出。干杯
推荐阅读
- http - 如何在更改会话 cookie 时防止竞争条件
- javascript - react-js 测试库无法测试 useState 钩子和异步 axios 登录功能
- docker - minikube - 为什么“docker”驱动程序不应该与 root 权限一起使用
- reactjs - 如何使用 react-chartjs-2 过滤和更新日期
- yq - 具有多个 env 运算符的 yq 查询
- jquery - 使用 jquery 添加多个单选按钮组
- javascript - 如何过滤数据名称并添加过滤数据的数量
- c - 如何在ebpf(XDP)内核程序中将字符串中的值分配给struct in6_addr?
- flutter - 您如何存储正确答案的数量以显示在测验应用程序的分数页面上?
- android - 在 SurfaceView 中截屏仅在保存时显示黑屏