Tkinter / Python Changing attributes of Tkinter Widgets

I am trying to figure out how to change widgets in Python/TK so that I can clear and refill listboxes also so that I can change the state of Buttons and other objects.  here is some example code.

from Tkinter import *

class Delivery:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

      self.mbar = Frame(frame, relief = 'raised', bd=2)
      self.mbar.pack(fill = X)

      # Create File menu
      self.filebutton = Menubutton(self.mbar, text = 'File')
      self.filebutton.pack(side = LEFT)

      self.filemenu = Menu(self.filebutton, tearoff=0)
      self.filebutton['menu'] = self.filemenu

      # Populate File menu
      self.filemenu.add('command', label = 'Exit', command = self.quit)

      # Create  help menu
      self.helpbutton = Menubutton(self.mbar, text = 'Help', )
      self.helpbutton.pack(side = RIGHT)

      self.helpmenu = Menu(self.helpbutton, tearoff=0)
      self.helpbutton['menu'] = self.helpmenu

      # Populate help menu
      self.helpmenu.add('command', label = 'help', command = self.stub)

        iframe1 = Frame(frame, bd=2, relief=SUNKEN)
        Button(iframe1, text='Button').pack(side=LEFT, padx=5)
        Checkbutton(iframe1, text='CheckButton').pack(side=LEFT, padx=5)

        v=IntVar()
        #Radiobutton(iframe1, text='Button', variable=v,
        #            value=3).pack(side=RIGHT, anchor=W)
        #Radiobutton(iframe1, text='Dio', variable=v,
        #            value=2).pack(side=RIGHT, anchor=W)
        #Radiobutton(iframe1, text='Ra', variable=v,
        #            value=1).pack(side=RIGHT, anchor=W)
        #iframe1.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='OFFENBACHERS Delivery Scheduling System').pack()
        t = StringVar()
        #Entry(iframe2, textvariable=t, bg='white').pack(side=RIGHT, padx=5)
        #t.set('Entry widget')
        iframe2.pack(expand=1, fill=X, pady=10, padx=140)

        dframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(dframe2, text='ENTER TICKET NUMBER:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(dframe2, textvariable=t, bg='white').pack(side=LEFT, pady=10)
        t.set('')
        Button(dframe2, text="SUBMIT",command = self.submitticket).pack(side=LEFT)
        dframe2.pack(expand=1, fill=X, pady=15, padx=140)

        zframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(zframe2, text='ENTER ZIP CODE:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(zframe2, textvariable=t, bg='white').pack(side=LEFT, pady=10)
        t.set('')
        Button(zframe2, text="SUBMIT").pack(side=LEFT)
        zframe2.pack(expand=1, fill=X, pady=5, padx=140)

        iframe3 = Frame(frame, bd=2, relief=GROOVE)
        listbox = Listbox(iframe3, state=DISABLED, height=4, width=30)
        scroll = Scrollbar(iframe3, command=listbox.yview)
        listbox.configure(yscrollcommand=scroll.set)
        for item in ['January','February','March','April','May','June','July','August','September','October','November','December']:
            listbox.insert(END, item)
        scroll.pack(side=LEFT, fill=Y)
        listbox.pack(fill=X)
        iframe3.pack(expand=1, fill=X, pady=10, padx=140)
       
        iframe4 = Frame(frame, bd=2, relief=GROOVE)
        listbox = Listbox(iframe4, state=DISABLED, height=15, width=30)
        scroll = Scrollbar(iframe4, command=listbox.yview)
        listbox.configure(yscrollcommand=scroll.set)
        for item in ['January','February','March','April','May','June','July','August','September','October','November','December']:
            listbox.insert(END, item)
        scroll.pack(side=LEFT, fill=Y)
        listbox.pack(fill=X)
        iframe4.pack(expand=1, fill=X, pady=10, padx=5)

        f1=Frame(frame, width=300, height=10)
        Button(f1, text="SCHEDULE DELIVERY", bg='gray75', state=DISABLED).pack(side=LEFT, padx=40)
        f1.pack()
       
        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='', width=300,
                relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=22, padx=5)

    def quit(self):
        root.destroy()

    def stub(self):
        pass

    def submitticket(self):
        pass
     
root = Tk()
root.option_add('*font', ('verdana', 10, 'bold'))
all = Delivery(root)
root.title('Delivery Scheduling')
root.mainloop()



In the Above example I wouldl like to make def submitticket change the iframe3 to state=Normal and as well I would like it to Clear the Items that are there and Add new ones to that listbox.....  

Thanks in advance
LVL 1
scogger1974Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RichieHindleCommented:
Here you go.  The changes are:

 o Keep references to your listboxes, as self.listbox1 and self.listbox2
 o Don't disable the listboxes until after you've populated them
 o In submitticket, enable the listbox, delete the items, and add new ones.

from Tkinter import *

class Delivery:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.mbar = Frame(frame, relief = 'raised', bd=2)
        self.mbar.pack(fill = X)

        # Create File menu
        self.filebutton = Menubutton(self.mbar, text = 'File')
        self.filebutton.pack(side = LEFT)
       
        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu
       
        # Populate File menu
        self.filemenu.add('command', label = 'Exit', command = self.quit)
       
        # Create  help menu
        self.helpbutton = Menubutton(self.mbar, text = 'Help', )
        self.helpbutton.pack(side = RIGHT)
       
        self.helpmenu = Menu(self.helpbutton, tearoff=0)
        self.helpbutton['menu'] = self.helpmenu
       
        # Populate help menu
        self.helpmenu.add('command', label = 'help', command = self.stub)
       
        iframe1 = Frame(frame, bd=2, relief=SUNKEN)
        Button(iframe1, text='Button').pack(side=LEFT, padx=5)
        Checkbutton(iframe1, text='CheckButton').pack(side=LEFT, padx=5)

        v=IntVar()
        #Radiobutton(iframe1, text='Button', variable=v,
        #            value=3).pack(side=RIGHT, anchor=W)
        #Radiobutton(iframe1, text='Dio', variable=v,
        #            value=2).pack(side=RIGHT, anchor=W)
        #Radiobutton(iframe1, text='Ra', variable=v,
        #            value=1).pack(side=RIGHT, anchor=W)
        #iframe1.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='OFFENBACHERS Delivery Scheduling System').pack()
        t = StringVar()
        #Entry(iframe2, textvariable=t, bg='white').pack(side=RIGHT, padx=5)
        #t.set('Entry widget')
        iframe2.pack(expand=1, fill=X, pady=10, padx=140)

        dframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(dframe2, text='ENTER TICKET NUMBER:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(dframe2, textvariable=t, bg='white').pack(side=LEFT, pady=10)
        t.set('')
        Button(dframe2, text="SUBMIT",command = self.submitticket).pack(side=LEFT)
        dframe2.pack(expand=1, fill=X, pady=15, padx=140)

        zframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(zframe2, text='ENTER ZIP CODE:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(zframe2, textvariable=t, bg='white').pack(side=LEFT, pady=10)
        t.set('')
        Button(zframe2, text="SUBMIT").pack(side=LEFT)
        zframe2.pack(expand=1, fill=X, pady=5, padx=140)

        iframe3 = Frame(frame, bd=2, relief=GROOVE)
        self.listbox1 = Listbox(iframe3, height=4, width=30)
        scroll = Scrollbar(iframe3, command=self.listbox1.yview)
        self.listbox1.configure(yscrollcommand=scroll.set)
        for item in ['January','February','March','April','May','June','July','August','September','October','November','December']:
            self.listbox1.insert(END, item)
        scroll.pack(side=LEFT, fill=Y)
        self.listbox1.pack(fill=X)
        self.listbox1.config(state=DISABLED)
        iframe3.pack(expand=1, fill=X, pady=10, padx=140)
       
        iframe4 = Frame(frame, bd=2, relief=GROOVE)
        self.listbox2 = Listbox(iframe4, height=15, width=30)
        scroll = Scrollbar(iframe4, command=self.listbox2.yview)
        self.listbox2.configure(yscrollcommand=scroll.set)
        for item in ['January','February','March','April','May','June','July','August','September','October','November','December']:
            self.listbox2.insert(END, item)
        scroll.pack(side=LEFT, fill=Y)
        self.listbox2.pack(fill=X)
        self.listbox2.config(state=DISABLED)
        iframe4.pack(expand=1, fill=X, pady=10, padx=5)

        f1=Frame(frame, width=300, height=10)
        Button(f1, text="SCHEDULE DELIVERY", bg='gray75', state=DISABLED).pack(side=LEFT, padx=40)
        f1.pack()
       
        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='', width=300,
                relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=22, padx=5)

    def quit(self):
        root.destroy()

    def stub(self):
        pass

    def submitticket(self):
        self.listbox1.config(state=NORMAL)
        self.listbox1.delete(0, END)
        for item in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']:
            self.listbox1.insert(END, item)
     
root = Tk()
root.option_add('*font', ('verdana', 10, 'bold'))
all = Delivery(root)
root.title('Delivery Scheduling')
root.mainloop()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scogger1974Author Commented:
And what if I want to make the listbox below that enable with the correct information based on what I clicked in the listbox that just got filled...  like I click January and it gives me all the available dates for january.  I already know how to interface it with MySql to get the data.. Just trying to learn how the events work and such...

0
RichieHindleCommented:
Here you are.  self.listbox1.bind('<ButtonRelease>', self.onselect) is the "how the events work and such" piece!

from Tkinter import *

class Delivery:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.mbar = Frame(frame, relief = 'raised', bd=2)
        self.mbar.pack(fill = X)

        # Create File menu
        self.filebutton = Menubutton(self.mbar, text = 'File')
        self.filebutton.pack(side = LEFT)
       
        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu
       
        # Populate File menu
        self.filemenu.add('command', label = 'Exit', command = self.quit)
       
        # Create  help menu
        self.helpbutton = Menubutton(self.mbar, text = 'Help', )
        self.helpbutton.pack(side = RIGHT)
       
        self.helpmenu = Menu(self.helpbutton, tearoff=0)
        self.helpbutton['menu'] = self.helpmenu
       
        # Populate help menu
        self.helpmenu.add('command', label = 'help', command = self.stub)
       
        iframe1 = Frame(frame, bd=2, relief=SUNKEN)
        Button(iframe1, text='Button').pack(side=LEFT, padx=5)
        Checkbutton(iframe1, text='CheckButton').pack(side=LEFT, padx=5)

        v=IntVar()
        #Radiobutton(iframe1, text='Button', variable=v,
        #            value=3).pack(side=RIGHT, anchor=W)
        #Radiobutton(iframe1, text='Dio', variable=v,
        #            value=2).pack(side=RIGHT, anchor=W)
        #Radiobutton(iframe1, text='Ra', variable=v,
        #            value=1).pack(side=RIGHT, anchor=W)
        #iframe1.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='OFFENBACHERS Delivery Scheduling System').pack()
        t = StringVar()
        #Entry(iframe2, textvariable=t, bg='white').pack(side=RIGHT, padx=5)
        #t.set('Entry widget')
        iframe2.pack(expand=1, fill=X, pady=10, padx=140)

        dframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(dframe2, text='ENTER TICKET NUMBER:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(dframe2, textvariable=t, bg='white').pack(side=LEFT, pady=10)
        t.set('')
        Button(dframe2, text="SUBMIT",command = self.submitticket).pack(side=LEFT)
        dframe2.pack(expand=1, fill=X, pady=15, padx=140)

        zframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(zframe2, text='ENTER ZIP CODE:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(zframe2, textvariable=t, bg='white').pack(side=LEFT, pady=10)
        t.set('')
        Button(zframe2, text="SUBMIT").pack(side=LEFT)
        zframe2.pack(expand=1, fill=X, pady=5, padx=140)

        iframe3 = Frame(frame, bd=2, relief=GROOVE)
        self.listbox1 = Listbox(iframe3, height=4, width=30)
        scroll = Scrollbar(iframe3, command=self.listbox1.yview)
        self.listbox1.configure(yscrollcommand=scroll.set)
        self.listbox1.bind('<ButtonRelease>', self.onselect)
        for item in ['January','February','March','April','May','June','July','August','September','October','November','December']:
            self.listbox1.insert(END, item)
        scroll.pack(side=LEFT, fill=Y)
        self.listbox1.pack(fill=X)
        self.listbox1.config(state=DISABLED)
        iframe3.pack(expand=1, fill=X, pady=10, padx=140)
       
        iframe4 = Frame(frame, bd=2, relief=GROOVE)
        self.listbox2 = Listbox(iframe4, height=15, width=30)
        scroll = Scrollbar(iframe4, command=self.listbox2.yview)
        self.listbox2.configure(yscrollcommand=scroll.set)
        for item in ['January','February','March','April','May','June','July','August','September','October','November','December']:
            self.listbox2.insert(END, item)
        scroll.pack(side=LEFT, fill=Y)
        self.listbox2.pack(fill=X)
        self.listbox2.config(state=DISABLED)
        iframe4.pack(expand=1, fill=X, pady=10, padx=5)

        f1=Frame(frame, width=300, height=10)
        Button(f1, text="SCHEDULE DELIVERY", bg='gray75', state=DISABLED).pack(side=LEFT, padx=40)
        f1.pack()
       
        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='', width=300,
                relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=22, padx=5)

    def quit(self):
        root.destroy()

    def stub(self):
        pass

    def submitticket(self):
        self.listbox1.config(state=NORMAL)
        self.listbox1.delete(0, END)
        for item in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']:
            self.listbox1.insert(END, item)
     
    def onselect(self, evt):
        item1 = self.listbox1.get(self.listbox1.curselection())
        self.listbox2.config(state=NORMAL)
        self.listbox2.delete(0, END)
        for item in ['Option 1 for '+item1, 'Option 2 for '+item1]:
            self.listbox2.insert(END, item)

root = Tk()
root.option_add('*font', ('verdana', 10, 'bold'))
all = Delivery(root)
root.title('Delivery Scheduling')
root.mainloop()
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.