简体   繁体   中英

Returning a selected value from a dynamically populated combobox as a global variable?

I'm trying to automate some work to process data that is brought in from SQL. Towards the start of my full code I found and altered the below class from elsewhere on the site to act as a smooth interface so others can use the code too.

The aim of this is to retrieve the desired 'MPAN' and a year from the options. The combo-boxes are populated exactly as I wished, however the problem comes when trying to retrieve the selected values as global variables, and close the GUI when clicking 'Submit', as it stands nothing happens and the values are not obtained, but there is no error produced.

My assumption would be I've made a mistake with my 'getMPAN' function, though I am not very experienced with this level of tkinter so would greatly apprciate a hand. For simplicity sake I have added the category dictionary to act as mock data.

b2=[[2018],[2019],[2020],[2021],[2022],[2023],[2024],[2025]]
category = {'home': ['utilities','rent','cable'],
    'car': ['gas','oil','repairs'],
    'rv':['parks','maintenance','payment']}
params=''
mpan=''
year=''

class Application(Frame):

    def __init__(self, master=None, Frame=None):
        Frame.__init__(self, master)
        super(Application,self).__init__()
        self.grid(column = 5,row = 20,padx = 50,pady = 50)
        self.createWidgets()

    def getUpdateData(self,  event):
        self.AccountCombo['values'] = category[self.CategoryCombo.get()]
        self.AccountCombo.current(0)

    def createWidgets(self):
        tk.Label(text = 'Area Code:').grid(row = 2,column = 1,padx = 10)
        tk.Label(text = 'MPAN:').grid(row = 4,column = 1,padx = 10)
        tk.Label(text = 'Year:').grid(row = 6, column = 1, padx = 10)
        self.AccountCombo = ttk.Combobox( width = 15)
        self.AccountCombo.grid(row = 5,column = 1,pady = 25,padx = 10)

        self.CategoryCombo = ttk.Combobox(width = 15,  values = list(category.keys()))
        self.CategoryCombo.bind('<<ComboboxSelected>>', self.getUpdateData)
        self.CategoryCombo.grid(row = 3,column = 1,padx = 10,pady = 25)

        self.YearCombo = ttk.Combobox(width = 15, values = b2)
        self.YearCombo.grid(row = 7, column = 1, padx = 10, pady = 25)
        self.YearCombo.current(0)

    def getMPAN(self):
        mpan=self.AccountCombo.get()
        year=self.YearCombo.get()
        self.destroy()

        global params
        params=[mpan,year]

w=tk.Button(Application(), text='Submit', command=Application().getMPAN)
w.grid(row=8, column=1)

app = Application()
app.master.title('MPAN Selector')
app.mainloop()

This is my first time posting on the site so I apologise if I'm missing any details. I've seen similar questions but none with solutions for this situation.

Figured out where I was going wrong.

Needed to bring the button inside the createWidgets function in order to correctly get the selected values from the ComboBox.

As to closing the widgets with the buttons command i simply needed to add the following line to the init :

self.master=master

Then in the getMPAN function changing the destroy to:

self.master.destroy()

I'm sure this is relatively sloppy fix but in total this was my code:

b2=[[2018],[2019],[2020],[2021],[2022],[2023],[2024],[2025]]
category = {'home': ['utilities','rent','cable'],
    'car': ['gas','oil','repairs'],
    'rv':['parks','maintenance','payment']}
params=''
mpan=''
year=''

class Application(tk.Frame):

    def __init__(self, master=None, Frame=None):
        Frame.__init__(self, master)
        self.master=master
        super(Application,self).__init__()
        self.grid(column = 5,row = 20,padx = 50,pady = 50)
        self.createWidgets()

    def getUpdateData(self,  event):
        self.AccountCombo['values'] = category[self.CategoryCombo.get()]
        self.AccountCombo.current(0)

    def createWidgets(self):
        tk.Label(text = 'Area Code:').grid(row = 2,column = 1,padx = 10)
        tk.Label(text = 'MPAN:').grid(row = 4,column = 1,padx = 10)
        tk.Label(text = 'Year:').grid(row = 6, column = 1, padx = 10)
        self.AccountCombo = ttk.Combobox( width = 15)
        self.AccountCombo.grid(row = 5,column = 1,pady = 25,padx = 10)

        self.CategoryCombo = ttk.Combobox(width = 15,  values = list(category.keys()))
        self.CategoryCombo.bind('<<ComboboxSelected>>', self.getUpdateData)
        self.CategoryCombo.grid(row = 3,column = 1,padx = 10,pady = 25)

        self.YearCombo = ttk.Combobox(width = 15, values = b2)
        self.YearCombo.grid(row = 7, column = 1, padx = 10, pady = 25)
        self.YearCombo.current(0)

        button=ttk.Button(self, text='Submit', command=self.getMPAN)
        button.grid(row=9, column=1)

    def getMPAN(self):
        mpan=self.AccountCombo.get()
        year=self.YearCombo.get()
        self.master.destroy()

        global params
        params=[mpan,year]

w=tk.Button(Application(), text='Submit', command=Application().getMPAN)
w.grid(row=8, column=1)

app = Application()
app.master.title('MPAN Selector')
app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM