Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Tkinter / Python  Changing attributes of Tkinter Widgets

Posted on 2007-04-06
3
723 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:scogger1974
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 18865271
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
 
LVL 1

Author Comment

by:scogger1974
ID: 18865321
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
 
LVL 14

Expert Comment

by:RichieHindle
ID: 18865513
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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question