[英]Python Tkinter - 2 combo boxes to filter dataset - first combobox lists states, second combobox lists only facilities in that state
new to Tkinter... I am trying to make a mini app in Tkinter where the user selects the state of a medical facility in one combobox and the specific name of a facility in a second combobox from that state. 現在,我已經能夠讓我的 dataframe 過濾到在 state (第一個組合框)中選擇的值,但也不是設施。 我只希望之前選擇的 state 的設施出現在第二個 combobox 中。 目前,所有設施都出現在第二個 combobox 中,無論在 combobox 1 中選擇的 state 是什么。我知道我的代碼是錯誤的,但我想在這里嘗試一下。
Just as an example, if I selected 'oh' for the state in combobox 1, I would only want 'acme facility' and '123 healthcare' to appear in combobox 2. From there, if I selected '123 healthcare', my dataframe將僅由俄亥俄州名為“123 醫療保健”的設施組成。
基本上,如果我只是在 pandas (但我需要在 Tkinter 中完成)和我的 IDE 命令中工作,我希望它這樣做:
newdf = df.loc[((df['state'] == combobox1value) & (df['provname'] == combobox2value))]
任何幫助表示贊賞。
import pandas as pd
from tkinter import ttk
import numpy as np
from tkinter import Tk
import tkinter as tk
# function to get unique values
def unique(list1):
x = np.array(list1)
print(np.unique(x))
def on_click():
val = n.get()
if val == 'all':
print(df)
else:
df2 = df[ df['state'] == val ]
print(df2)
def on_click2():
val = n2.get()
if val == 'all':
print(df)
else:
df2 = df[ df['provname'] == val ]
print(df2)
df2.to_excel('test.xlsx')
df = pd.DataFrame({
'logdate': ['1/1/2020','1/2/2022', '1/3/2022', '3/3/2021','5/13/2021','10/11/2019','1/5/2020'],
'state': ['oh','oh', 'oh', 'ar','mi','ma','ms'],
'provname': ['acme facility','123 healthcare', '123 healthcare','basic clinic','healthcare solutions','best clinic','one stop clinic'],
})
values = ['all'] + list(df['state'].unique())
dfx = df.loc[((df['state'] == 'oh') & (df['provname'] == '123 healthcare'))]
print(dfx)
# Creating tkinter window and set dimensions
window = tk.Tk()
window.title('Combobox')
window.geometry('500x250')
# label text for title
ttk.Label(window, text = "Main Menu",
background = 'cyan', foreground ="black",
font = ("Times New Roman", 15)).grid(row = 0, column = 1)
#FILTER STATE - PART 1
# Set label
ttk.Label(window, text = "Select the State :",
font = ("Times New Roman", 12)).grid(column = 0,
row = 5, padx = 6, pady = 25)
# Create Combobox
n = tk.StringVar()
state = ttk.Combobox(window, width = 27, textvariable = n)
# Adding combobox drop down list
state['values'] = list(df['state'].unique())
state.grid(column = 1, row = 5)
state.current()
#FILTER FACILITY - PART 2
# Set label
ttk.Label(window, text = "Select the Facility :",
font = ("Times New Roman", 12)).grid(column = 0,
row = 6, padx = 6, pady = 25)
ttk.Button(window, text = "OK", command=on_click).grid(column = 2,
row = 6, padx = 5, pady = 25)
# Create Combobox
n2 = tk.StringVar()
prov = ttk.Combobox(window, width = 27, textvariable = n2)
# Adding combobox drop down list
prov['values'] = list(df['provname'].unique())
prov.grid(column = 1, row = 6)
prov.current()
window.mainloop()
當您 select 您的 state 時,您需要更新您的第二個 combobox 中的值。 為此,您需要將第一個 combobox 中的"<<ComboboxSelected>>"
事件與更改第二個 combobox 值的 function 綁定。
# Function for when first combobx selected
def state_selected(event):
state_value = state.get()
facilities = list(df.loc[df['state']==state_value]['provname'].unique())
if facilities:
prov['values']=facilities
else:
prov['values']=""
# Bind for when your first combobox is selected
state.bind("<<ComboboxSelected>>", state_selected)
在腳本中插入上面的代碼片段會給出:
import pandas as pd
from tkinter import ttk
import numpy as np
from tkinter import Tk
import tkinter as tk
# function to get unique values
def unique(list1):
x = np.array(list1)
print(np.unique(x))
def on_click():
val = n.get()
if val == 'all':
print(df)
else:
df2 = df[ df['state'] == val ]
print(df2)
def on_click2():
val = n2.get()
if val == 'all':
print(df)
else:
df2 = df[ df['provname'] == val ]
print(df2)
df2.to_excel('test.xlsx')
df = pd.DataFrame({
'logdate': ['1/1/2020','1/2/2022', '1/3/2022', '3/3/2021','5/13/2021','10/11/2019','1/5/2020'],
'state': ['oh','oh', 'oh', 'ar','mi','ma','ms'],
'provname': ['acme facility','123 healthcare', '123 healthcare','basic clinic','healthcare solutions','best clinic','one stop clinic'],
})
values = ['all'] + list(df['state'].unique())
dfx = df.loc[((df['state'] == 'oh') & (df['provname'] == '123 healthcare'))]
print(dfx)
# Creating tkinter window and set dimensions
window = tk.Tk()
window.title('Combobox')
window.geometry('500x250')
# label text for title
ttk.Label(window, text = "Main Menu",
background = 'cyan', foreground ="black",
font = ("Times New Roman", 15)).grid(row = 0, column = 1)
#FILTER STATE - PART 1
# Set label
ttk.Label(window, text = "Select the State :",
font = ("Times New Roman", 12)).grid(column = 0,
row = 5, padx = 6, pady = 25)
# Create Combobox
n = tk.StringVar()
state = ttk.Combobox(window, width = 27, textvariable = n)
# Adding combobox drop down list
state['values'] = list(df['state'].unique())
state.grid(column = 1, row = 5)
state.current()
#FILTER FACILITY - PART 2
# Set label
ttk.Label(window, text = "Select the Facility :",
font = ("Times New Roman", 12)).grid(column = 0,
row = 6, padx = 6, pady = 25)
ttk.Button(window, text = "OK", command=on_click).grid(column = 2,
row = 6, padx = 5, pady = 25)
# Create Combobox
n2 = tk.StringVar()
prov = ttk.Combobox(window, width = 27, textvariable = n2)
# Function for when first combobx selected
def state_selected(event):
state_value = state.get()
facilities = list(df.loc[df['state']==state_value]['provname'].unique())
if facilities:
prov['values']=facilities
else:
prov['values']=""
# Bind for when your first combobox is selected
state.bind("<<ComboboxSelected>>", state_selected)
prov.grid(column = 1, row = 6)
prov.current()
window.mainloop()
對您的代碼的更多評論:
prov.current()
不會做任何事情,因為您沒有選擇索引from tkinter import Tk
因為您已經導入 tkinter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.