简体   繁体   English

从动态填充的组合框中返回选定的值作为全局变量?

[英]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.我正在尝试自动化一些工作来处理从 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.这样做的目的是从选项中检索所需的“MPAN”和年份。 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.组合框完全按照我的意愿填充,但是当尝试将选定的值检索为全局变量时出现问题,并在单击“提交”时关闭 GUI,因为它没有任何反应并且未获得值,但是有没有错误产生。

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.我的假设是我在我的“getMPAN”函数上犯了一个错误,尽管我对这种级别的 tkinter 不是很有经验,所以我会非常感激。 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.需要将按钮放入 createWidgets 函数中,以便从 ComboBox 正确获取所选值。

As to closing the widgets with the buttons command i simply needed to add the following line to the init :至于使用按钮命令关闭小部件,我只需要将以下行添加到init

self.master=master

Then in the getMPAN function changing the destroy to:然后在 getMPAN 函数中将 destroy 更改为:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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