如何选择列表框并根据该选择创建第二个列表框?

时间:2014-07-30 21:04:38

标签: python python-2.7 python-3.x tkinter tk

所以我还在学习tkinter。我有三个列表框,第一个有一定数量的选项。当用户选择选项时,第二个列表框需要根据先前的选择进行填充。这是我试过的代码。我正在使用python 2.7 更新:

import sys
import Tkinter as TK

font = ('Times New Rome', 12, 'Bold')
Template = ''
Entrybox1 = ''
Entrybox2 = ''
Lb1 = ''
Lb2 = ''
Lb3 = ''

class smartstopkiosk_tk(TK.Tk):
    def __init__(self, parent):
        TK.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()
        #self.parent.title("Smartstop Kiosk")
        global Entrybox1
        global Entrybox2
        global LocalTemplate
        global Software
        seltext = ""
        software = ('Microsoft', 'Abode', 'SAP', 'Lotus  Notes', 'Jive','Cisco',     'Chrome')
        hardware = ("Computer",  "Charger", " USB Headset", "Keyboard", "Mouse", "Webcam")
        microsoft = ("Outlook", "Lync", "Word", "Excel", "PowerPoint", "Visio", "Project", "Publisher")
        adobe = ("Adobe Reader", "Java", "Flash", "Adobe Pro")
        cisco = ('IP Communicator', '')
        wireless = ("MYLOW",  "Corporate", "Visitor", "MYLTW")
        printers  = ("2N", "2S", "3N", "3S", "4N",  "4S",  "5N", "5S")      
        mobile =  ("Blackberry", "iPhone","iPad")
        AD  = ("Unlock", "Reset")
        LocalTemplate = ('User Assistance with Software.', 'User Assistance with       Hardware.', 'Mobile Device Support.',
        'Software Installation Request', 'Hardware Request', 'Wireless Connection',   'Password Reset/Username Unlocked', 
        'Add Printer')

         #creates font types
         font = ('Times New Roman', 14, 'bold')
         font2 = ('Times New Roman',12, 'bold')

         #Creates Instructions for Users
         stepOne = TK.LabelFrame(self, text=" 1. User Information: ", font = font2)
         stepOne.grid(row=0, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5,   ipady=5)
         stepTwo = TK.LabelFrame(self, text="2. Select Program or Hardware Problems", font = font2)
         stepTwo.grid(row=3, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
         stepThree = TK.LabelFrame(self, text="Please Enter the Problem or Your Request:", font = font2)
         stepThree.grid(row=6, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)

         #Creates Label for Users
         Label1 = TK.Label(stepOne, text = "First Name", font = font)
         Label1.grid(column = 2, row = 0, sticky = 'w', padx = (10, 10))
         Label2 = TK.Label(stepOne, text = "Last Name", font = font)
         Label2.grid(column = 4, row = 0, sticky = 'w', padx = (10, 10))
         Label3 = TK.Label(stepOne, text = "UserName", font = font)
         Label3.grid(column = 6, row = 0, sticky = 'w', padx = (10, 10))

         #Creates Entry Box for User input
         self.entry = TK.Entry(stepOne, width = 30)
         self.entry.grid(column = 2, row = 1,padx = (10, 10))
         entry2 = TK.Entry(stepOne, width = 30)
         entry2.grid(column = 4, row = 1,padx = (10, 10))
         entry3 = TK.Entry(stepOne, width = 30)
         entry3.grid(column = 6, row = 1, columnspan = 2, padx = (10, 10))

         #Creates User Input Box
         Usertext = TK.Text(stepThree, height = 10, width = 110, font = font)
         Usertext.grid(column = 2, row = 15, padx = (10, 10))

         #Creates List Boxes
         Listbox1 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font   = font, exportselection = 0)
         Listbox1.grid(column = 2, row = 1, padx = (10, 10))
         for i in LocalTemplate:
             Listbox1.insert(TK.END, i)
         Listbox1.bind("<<ListboxSelect>>", self.selection)
         Listbox2 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
         Listbox2.grid(column = 4, row = 1, padx = (10, 10))
         for x in seltext:
             Listbox2.insert(TK.END, x)
         Listbox2.bind("<<ListboxSelect>>", self.selection)
         Listbox3 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
         Listbox3.grid(column = 6, row = 1, padx = (10, 10))

         #Creates Buttons
         Submit = TK.Button(self, text = 'Submit', font = font2)
         Submit.grid(column = 3, row = 10, sticky = 'we', padx = (10, 10))
         Startover = TK.Button(self, text = 'Cancel', font = font2)
         Startover.grid(column = 4, row = 10, sticky = 'we', padx = (10, 10))

         def selection(self, val):
         sender = val.Listbox1
         index = listbox1.curselection()
         value = Listbox1.get(index[0])
         if index == 1 or index == 4:
             seltext = software
         elif index == 2 or index == 5:
             seltext = hardware
         elif index == 3:
             seltext = mobile
         elif index == 6:
             seltext = wireless
         elif index == 7:
             seltext = AD
         elif index == 8:
             seltext = printer

def main():
    app = smartstopkiosk_tk(None)
    app.geometry("1300x768")

    app.mainloop()

if __name__ == "__main__":
    main()

我一直收到此错误Traceback       文件&#34; C:\ python27 \ lib \ lib-tk \ Tkinter.py&#34;,第1845行, getattr     return getattr(self.tk,attr) AttributeError:选择

我很感激帮助!!

已更新! 以下是我如何在第一个列表框中显示文本并根据您的选择填写第二个列表框。

    def getchoice(event):
        seltext = ''
        INDEX = Listbox1.curselection()
        if INDEX == (0,) or INDEX == (4,):
            seltext = software
        elif INDEX == (1,) or INDEX == (5,):
            seltext = hardware

        elif INDEX == (2,):
            seltext = mobile

        elif INDEX == (3,):
            seltext = wireless

        elif INDEX == (6,):
            seltext = AD

        elif INDEX == (7,):
            seltext = printers
        print INDEX
        Listbox2.delete(0, TK.END)
        for x in seltext:
            Listbox2.insert(TK.END, x)  

        #Creates List Boxes
    Listbox1 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
    Listbox1.grid(column = 2, row = 1, padx = (10, 10))
    for x in LocalTemplate:
        Listbox1.insert(TK.END, x)
    Listbox1.bind("<<ListboxSelect>>", getchoice)
    Listbox2 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
    Listbox2.grid(column = 4, row = 1, padx = (10, 10))
    Listbox2.bind("<<ListboxSelect>>", getchoice2)
    Listbox3 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font, exportselection = 0)
    Listbox3.grid(column = 6, row = 1, padx = (10, 10))

2 个答案:

答案 0 :(得分:0)

此行是问题所在,但错误消息可能告诉您:

value = widget.get(selection[0])

您没有名为selection的变量。也许您打算使用index[0]代替selection[0],因为您在前一行上设置了index

当然,你会遇到与widget类似的问题,这个问题也是未定义的。

答案 1 :(得分:0)

好吧@ Jobes23,这很有趣。现在您的代码中出现了一些“noobish错误”:一开始您已经定义了lbl123,而是使用了label123,使lbl1无效,有一些不必要的变量和添加的代码,同时为有时使用的列表框''和其他""设置变量围绕这些词,这不会影响代码,但是在整个代码中使用相同的约定是好的,它确实出现了(就像Bryan Oakley所说的那样)你刚刚将来自世界各地的一些代码组合起来,而不考虑它们如何单独和共同工作 - 了解能够解决问题的代码至关重要。我试图摆脱不必要的代码,但我不确定你需要多少代码。

现在我早些时候说self.selection不是一个选项,而是我摆脱了listbox.bind(...)并用一个命令构成一个按钮,该命令导致了一个带有所选选项并使用的函数将第二个列表框设置为字符串的结果,这是我想要实现的目标。

import Tkinter as TK

class smartstopkiosk_tk(TK.Tk):
    def __init__(self, parent):
        TK.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()
        #self.parent.title("Smartstop Kiosk")
        software = ('Microsoft', 'Abode', 'SAP', 'Lotus  Notes', 'Jive','Cisco', 'Chrome')
        hardware = ("Computer",  "Charger", " USB Headset", "Keyboard", "Mouse", "Webcam")
        microsoft = ("Outlook", "Lync", "Word", "Excel", "PowerPoint", "Visio", "Project", "Publisher")
        adobe = ("Adobe Reader", "Java", "Flash", "Adobe Pro")
        cisco = ('IP Communicator', '')
        wireless = ("MYLOW",  "Corporate", "Visitor", "MYLTW")
        printers  = ("2N", "2S", "3N", "3S", "4N",  "4S",  "5N", "5S")
        mobile =  ("Blackberry", "iPhone","iPad")
        AD  = ("Unlock", "Reset")
        LocalTemplate = ('User Assistance with Software', 'User Assistance with Hardware', 'Mobile Device Support', 'Software Installation Request', 'Hardware Request', 'Wireless Connection', 'Password Reset/Username Unlocked',
    'Add Printer')
        def getchoice2():
            print
            # use same method to find out which of the second list is chosen, and carry on.

        def getchoice():
            seltext = ''
            INDEX = Listbox1.curselection()
            if INDEX == ('0',) or INDEX == ('4',):
                seltext = software
            if INDEX == ('1',) or INDEX == ('5',):
                seltext = hardware
            if INDEX == ('2',):
                seltext = mobile
            if INDEX == ('3',):
                seltext = wireless
            if INDEX == ('6',):
                seltext = AD
            if INDEX == ('7',):
                seltext = printers
            selectchoice.config(command = getchoice2)
            for x in seltext:
                Listbox2.insert(TK.END, x)


        #creates font types
        font = ('Times New Roman', 14, 'bold')
        font2 = ('Times New Roman',12, 'bold')
        #Creates Instructions for Users
        stepOne = TK.LabelFrame(self, text=" 1. User Information: ", font = font2)
        stepOne.grid(row=0, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5,   ipady=5)
        stepTwo = TK.LabelFrame(self, text="2. Select Program or Hardware Problems", font = font2)
        stepTwo.grid(row=3, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
        stepThree = TK.LabelFrame(self, text="Please Enter the Problem or Your Request:", font = font2)
        stepThree.grid(row=6, columnspan=7, sticky='w', padx=5, pady=5, ipadx=5, ipady=5)
        #Creates Label for Users
        Label1 = TK.Label(stepOne, text = "First Name", font = font)
        Label1.grid(column = 2, row = 0, sticky = 'w', padx = (10, 10))
        Label2 = TK.Label(stepOne, text = "Last Name", font = font)
        Label2.grid(column = 4, row = 0, sticky = 'w', padx = (10, 10))
        Label3 = TK.Label(stepOne, text = "Mylan UserName", font = font)
        Label3.grid(column = 6, row = 0, sticky = 'w', padx = (10, 10))
        #Creates Entry Box for User input
        self.entry = TK.Entry(stepOne, width = 30)
        self.entry.grid(column = 2, row = 1,padx = (10, 10))
        entry2 = TK.Entry(stepOne, width = 30)
        entry2.grid(column = 4, row = 1,padx = (10, 10))
        entry3 = TK.Entry(stepOne, width = 30)
        entry3.grid(column = 6, row = 1, columnspan = 2, padx = (10, 10))
        #Creates User Input Box
        Usertext = TK.Text(stepThree, height = 10, width = 110, font = font)
        Usertext.grid(column = 2, row = 15, padx = (10, 10))
        #Creates List Boxes
        Listbox1 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font   = font)
        Listbox1.grid(column = 2, row = 1, padx = (10, 10))
        for i in LocalTemplate:
            Listbox1.insert(TK.END, i)
        Listbox2 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font)
        Listbox2.grid(column = 4, row = 1, padx = (10, 10))
        Listbox3 = TK.Listbox(stepTwo, selectmode = 'SINGLE', height = 10, width = 35, font = font)
        Listbox3.grid(column = 6, row = 1, padx = (10, 10))
        #Creates Buttons
        selectchoice = TK.Button(self, text = 'select my choice', font = font2, command = getchoice)
        selectchoice.grid(row = 5, column = 1, sticky = 'we', padx = (10, 10))
        Submit = TK.Button(self, text = 'Submit', font = font2)
        Submit.grid(column = 3, row = 10, sticky = 'we', padx = (10, 10))
        Startover = TK.Button(self, text = 'Cancel', font = font2)
        Startover.grid(column = 4, row = 10, sticky = 'we', padx = (10, 10))

def main():
    app = smartstopkiosk_tk(None)
    app.geometry("1300x768")
    app.mainloop()

if __name__ == "__main__":
    main()

我希望这对你有所帮助,这是我解决过的更持续的问题之一,但真的是一个很好的挑战,所以欢呼。