idgara73612
idgara73612

Reputation: 13

Python3 tkinter combobox default value

I know this has been asked before, many time, but I still cannot get my code to work...

I am trying to get 7 dropdown lists to appear on the screen, with default values selected for each. I can get the boxes themselves to display, and the values I defined are in the list when you pull-down each one, but I cannot get them to default with any of the values when my gui loads. I read some post about garbage-collecting being an issue, so I followed that poster's recommendation on placing it inside a class. This is it...

class DropDown(object):
    def __init__(self, parent):
        self.parent = parent

    def tapoptions(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value, state="readonly")
        self.box['values'] = ('Empty','Jack','Coke','Tonic')
        self.box.current(1)

This is all housed inside a multi-frame window, which I copied from some source online (which I don't remember where at the moment), so I don't know if that is hindering my progress here or not. If I need to add some more of that code I can, but here is the bit where my DropDown is called...

class OnTap(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        body = tk.Frame(self)
        body.grid(row=1, column=1, sticky="ew")
        tappos = [0 for x in range(7)]
        for a in range(7):
            l = ttk.Label(body, text="Bottle "+str(a+1)+": ")
            l.grid(row=a+1, column=0)
            tappos[a] = DropDown(body)
            tappos[a].tapoptions()
            tappos[a].box.grid(row=a+1, column=1)

At some point I will link this to a DB, so when you load it up, it tells you what bottle is in each position, but right now, all I want to do is just have it show up with "Jack" in each slot when you go to that page, instead of the [blank]s I have now.

I am not a noob to programming in general, but I am when it comes to OOP.

Also, I rewrote this here, so if there are typos that you think might be the issue, please let me know, but there is also a chance that I just transcribed it incorrectly, and introduced those here, and not in my actual code.

Upvotes: 1

Views: 2192

Answers (2)

glls
glls

Reputation: 2403

As is, your code is unverifiable, leaving a lot of questions, but by cleaning up your code a bit you can make your Combobox easily reusable and have your element position returned accordingly.

from tkinter import ttk, Tk, Frame


class DropDown(ttk.Combobox):

    def set_list(self, argslist):
        self['values'] = argslist
        self.current(1)
        self.pack()


class OnTap:

    def __init__(self, parent):
        self.parent = parent
        body = Frame(self.parent)
        body.grid(row=1, column=1, sticky="ew")
        list1 = ['Empty', 'Jack', 'Coke', 'Tonic']

        tappos = [0 for x in range(7)]
        for i in range(5):
            l = ttk.Label(body, text="Bottle "+str(i+1)+": ")
            l.pack()
            tappos[i] = DropDown(body).set_list(list1)


if __name__ == '__main__':
    root = Tk()
    app = OnTap(root)
    root.mainloop()

Additionally, I don't know if you plan on having different values assigned to each Combobox, but this way will makes things easier on the longrun.

Upvotes: 2

Stevo Mitric
Stevo Mitric

Reputation: 1619

You said it yourself, "garbage-collecting". So instead of tappos, put self.tappos.

#tappos = [0 for x in range(7)]
self.tappos = [0 for x in range(7)]
...
#tappos[a] = DropDown(root)      
self.tappos[a] = DropDown(root)    # And so on. You get the picture.
...

Upvotes: 3

Related Questions