Multiple Optionmenus

Post here if you need help with creating a Graphical User Interface in Python.

Multiple Optionmenus

Postby JessiT » Tue Jun 25, 2013 10:15 pm

I am having trouble making dynamic optionmenus. I would like a selected value in the first optionmenu to be removed from the possible options for the second optionmenu, or vice versa. At the time of the selection for the first optionmenu, I would like the value to be added to a Listbox, if I revise my selection for the first optionmenu, it will be added to the second row in the Listbox. The remove selected button would remove a selected row from the Listbox. Any help would be appreciated. Thank you.

Code: Select all
import Tkinter, Tkconstants, tkFileDialog
import os

class Estimator(Tkinter.Frame):
 
    def __init__(self, parent):
        Tkinter.Frame.__init__(self, parent, background="white")   
         
        self.parent = parent
        self.parent.title("Fatality Estimator")


        lineFat = "A,B,C,D,E,F,G,H,I"
        Fat = lineFat.split(',')

        self.variableFat = Tkinter.StringVar(self)
        self.variableFat.set("") # default value
        self.variableFat1 = Tkinter.StringVar(self)
        self.variableFat1.set("") # default value

        self.FatDrop1 = Tkinter.OptionMenu(self, self.variableFat1, *Fat)
        self.FatDrop1.grid(sticky="w"+"e",row=3, column=1, padx=5, pady=5)
       
        self.FatDrop = Tkinter.OptionMenu(self, self.variableFat, *Fat)
        self.FatDrop.grid(sticky="w"+"e",row=1, column=1, padx=5, pady=5)

        self.FatButton = Tkinter.Button(self, text="Remove Selected", command= self.DeleteFatModel)
        self.FatButton.grid(sticky="w"+"e",row=2, column=1,  padx=5, pady=5)
 
        self.FatCopy = Tkinter.Listbox(self, border=4, height=5)
        self.FatCopy.grid(sticky="w"+"e",  row=1, column=2, rowspan=3, padx=5, pady=5)
        self.FatCopy.insert(Tkinter.END, self.variableFat1.get())

    def DeleteFatModel(self):
        self.FatCopy.delete(0, Tkinter.END)     

if __name__=='__main__':
    root = Tkinter.Tk()
    Estimator(root).grid()
    root.mainloop()
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm

Re: Multiple Optionmenus

Postby Yoriz » Wed Jun 26, 2013 12:18 am

I dont really know tkinter but this code shows using sets to maintain the two menu options.
Code: Select all
class SelectItems(object):
    def __init__(self):
        self.choices = set('ABCDEFGHI')
        self.items1 = self.choices
        self.items2 = set()

    def menu1Select(self, item):
        if item:
            self.items2.add(item)
            self.items1 = self.choices.symmetric_difference(self.items2)

    def menu2Select(self, item):
        if item:
            self.items2.remove(item)
            self.items1 = self.choices.symmetric_difference(self.items2)

    def __str__(self):
        items1 = ','.join(sorted(self.items1))
        items2 = ','.join(sorted(self.items2))
        return 'SelectableItems: {}\nSelectedItems: {}'.format(items1,
                                                               items2)


selectedItems = SelectItems()
print selectedItems
selectedItems.menu1Select('B')
print selectedItems
selectedItems.menu1Select('F')
print selectedItems
selectedItems.menu2Select('B')
print selectedItems

Output
Code: Select all
SelectableItems: A,B,C,D,E,F,G,H,I
SelectedItems:
SelectableItems: A,C,D,E,F,G,H,I
SelectedItems: B
SelectableItems: A,C,D,E,G,H,I
SelectedItems: B,F
SelectableItems: A,B,C,D,E,G,H,I
SelectedItems: F
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 723
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Multiple Optionmenus

Postby JessiT » Wed Jun 26, 2013 1:45 am

That helped, thank you. Here is the updated code, it still isn't updating the optionmenu correctly.
Code: Select all
import Tkinter, Tkconstants, tkFileDialog
import os

class Estimator(Tkinter.Frame):
 
    def __init__(self, parent):
        Tkinter.Frame.__init__(self, parent, background="white")   
         
        self.parent = parent
        self.parent.title("Fatality Estimator")


        lineFat = "A,B,C,D,E,F,G,H,I"
        self.Fat = lineFat.split(',')
        self.Empty = Tkinter.StringVar(self)
        self.Empty.set("")

        self.variableFat = Tkinter.StringVar(self)
        self.variableFat.set("") # default value
        self.variableFat1 = Tkinter.StringVar(self)
        self.variableFat1.set("") # default value

        self.FatDrop1 = Tkinter.OptionMenu(self, self.variableFat1, *self.Fat, command=self.menu1Select)
        self.FatDrop1.grid(sticky="w"+"e",row=3, column=1, padx=5, pady=5)
       
        self.FatDrop = Tkinter.OptionMenu(self, self.variableFat, *self.Fat, command=self.menu2Select)
        self.FatDrop.grid(sticky="w"+"e",row=1, column=1, padx=5, pady=5)

        self.FatButton = Tkinter.Button(self, text="Remove Selected", command= self.DeleteFatModel)
        self.FatButton.grid(sticky="w"+"e",row=2, column=1,  padx=5, pady=5)
 
        self.FatCopy = Tkinter.Listbox(self, border=4, height=5)
        self.FatCopy.grid(sticky="w"+"e",  row=1, column=2, rowspan=3, padx=5, pady=5)


    def menu1Select(self, variableFat1):
        if self.variableFat1:
            self.Empty.set(self.variableFat1.get())
            self.Fat.remove(self.Empty.get())
            self.FatCopy.insert(Tkinter.END, self.Empty.get())

    def menu2Select(self ,variableFat):
        if self.variableFat:
            self.Empty.set(self.variableFat.get())
            self.Fat.remove(self.Empty.get())
            self.FatCopy.insert(Tkinter.END, self.Empty.get())

    def DeleteFatModel(self):
        self.FatCopy.delete(0, Tkinter.END)     

if __name__=='__main__':
    root = Tkinter.Tk()
    Estimator(root).grid()
    root.mainloop()
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest