質問

Hi to all that look and might be able to help. I'm new to Python, and to coding in general. I'm working on a program that the basic concept of it is to have a few lists, make a selection from one list, and then another list is populated based on that first choice. I have been able to create the GUI, I've been able to create various different forms of "Listbox", I've been able to follow tutorials that show how to retrieve the indicies of that "Listbox", but I haven't been able to get the 2nd Listbox to fill in with the list I created for a choice made in the 1st box. I've tried checkboxes too, I did get closer on that one, but still no dice.

The supplied code below works in the sense that I can print the 2nd list into the python shell, but when I get a listbox set up in my GUI I can't get it to populate there. Nothing every happens. And I know that I don't have a listbox in this code to have it be populated with the 2nd list. So in this code, I would like the 'brand' choice if 'Apples', to populate the list from 'modelA' in a listbox. Or if the checkboxes actually weren't there and the items in 'brand' where in their own listbox. Any direction might help a lot more than what I've been able to do. Thanks again.

Python 3.3.2 Mac OS X 10.8.5

    from tkinter import *

    #brand would be for first listbox or checkboxes
    brand = ['Apples','Roses', 'Sonic', 'Cats']

    #model* is to be filled into the 2nd listbox, after selection from brand
    modelA = ['Ants', 'Arrows', 'Amazing', 'Alex']
    modelR = ['Early', 'Second', 'Real']
    modelS= ['Funny', 'Funky']
    modelC= ['Cool', 'Daring', 'Double']

    #create checkboxes 
    def checkbutton_value():
        if(aCam.get()):
            print("Models are: ", modelA)

        if(rCam.get()):
            print("Models are: ", modelR)

        if(sCam.get()):
            print("Models are: ", modelS)

        if(cCam.get()):
            print("Models are: ", modelC)

    #create frame, and check checkbuttons state, print value of model
    root = Tk()
    aCam = IntVar()
    rCam = IntVar()
    sCam = IntVar()
    cCam = IntVar()

    #Checkbutton functions
    apples = Checkbutton(root, text = "Apples", variable=aCam, command=checkbutton_value)
    apples.pack(anchor="w")
    roses = Checkbutton(root, text = "Roses", variable=rCam, command=checkbutton_value)
    roses.pack(anchor="w")
    sonic = Checkbutton(root, text = "Sonic", variable=sCam, command=checkbutton_value)
    sonic.pack(anchor="w")
    cats = Checkbutton(root, text = "Cats", variable=cCam, command=checkbutton_value)
    cats.pack(anchor="w")

    #general stuff for GUI
    root.title('My Brand')
    root.geometry("800x300")
    root.mainloop()
役に立ちましたか?

解決

To populate a Listbox based on the selection of another Listbox, you need to bind a method to the 1st Listbox's selection. Here's an example using makes/models of cars:

import Tkinter


class Application(Tkinter.Frame):
    def __init__(self, master):
        Tkinter.Frame.__init__(self, master)
        self.master.minsize(width=512, height=256)
        self.master.config()
        self.pack()

        self.main_frame = Tkinter.Frame()
        self.main_frame.pack(fill='both', expand=True)

        self.data = {
            'Toyota': ['Camry', 'Corolla', 'Prius'],
            'Ford': ['Fusion', 'Focus', 'Fiesta'],
            'Volkswagen': ['Passat', 'Jetta', 'Beetle'],
            'Honda': ['Accord', 'Civic', 'Insight']
        }

        self.make_listbox = Tkinter.Listbox(self.main_frame)
        self.make_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)

        # here we bind the make listbox selection to our method
        self.make_listbox.bind('<<ListboxSelect>>', self.load_models)

        self.model_listbox = Tkinter.Listbox(self.main_frame)
        self.model_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)

        # insert our items into the list box
        for i, item in enumerate(self.data.keys()):
            self.make_listbox.insert(i, item)

    def load_models(self, *args):
        selection = self.make_listbox.selection_get()

        # clear the model listbox
        self.model_listbox.delete(0, Tkinter.END)

        # insert the models into the model listbox
        for i, item in enumerate(self.data[selection]):
            self.model_listbox.insert(i, item)

root = Tkinter.Tk()
app = Application(root)
app.mainloop()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top