1

こんにちは、お役に立てるかもしれません。私はPythonとコーディング全般に不慣れです。私は、いくつかのリストを持ち、1 つのリストから選択し、その最初の選択に基づいて別のリストにデータが入力されるという基本的な概念のプログラムに取り組んでいます。GUI を作成できました。さまざまな形式の「リストボックス」を作成できました。その「リストボックス」のインデックスを取得する方法を示すチュートリアルに従うことができましたが、 2番目のリストボックスに、最初のボックスで行った選択のために作成したリストを入力することができました. チェックボックスも試してみましたが、それに近づきましたが、まだサイコロはありません。

以下の提供されたコードは、2 番目のリストを Python シェルに出力できるという意味で機能しますが、GUI でリストボックスを設定すると、そこに入力することができません。何も起こりません。そして、このコードにはリストボックスがなく、2番目のリストが入力されていないことがわかっています。したがって、このコードでは、「ブランド」の選択肢が「りんご」の場合、リストボックスに「modelA」からリストを入力する必要があります。または、チェックボックスが実際に存在せず、「ブランド」のアイテムが独自のリストボックスにある場合。どんな方向性でも、私ができることよりもはるかに役立つかもしれません. 再度、感謝します。

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

1 に答える 1

1

別のリストボックスの選択に基づいてリストボックスを作成するには、メソッドを最初のリストボックスの選択にバインドする必要があります。車のメーカー/モデルを使用した例を次に示します。

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()
于 2013-10-31T22:06:53.630 に答える