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. Right now, I've been able to work get my dataframe to filter to the values selected in the state (first combobox), but not the facility too. I only want facilities for the previously selected state to appear in the second combobox. Currently all of the facilities appear in the second combobox, regardless of the state selected in combobox 1. I know my code is wrong, but I'm trying to show that I'm making an attempt here.
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 would just consist of facilities in Ohio named '123 healthcare.'
Basically, if I were just working in pandas (but I need to do it in Tkinter) and my IDE command, I would want it to do this:
newdf = df.loc[((df['state'] == combobox1value) & (df['provname'] == combobox2value))]
Any help is appreciated.
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()
CodePudding user response:
You need to update the values in your second combobox when you select your state. To do this, you need to bind the "<<ComboboxSelected>>" event in the first combobox with a function that changes the second combobox values.
# 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)
Inserting the above snippet in your script gives:
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()
A few more comments on your code:
prov.current()won't do anything because you haven't selected an index- You don't need
from tkinter import Tkbecause you're already imported tkinter
