By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

How to add a drop down box in Python GUI

P: 17
Hi,

How do i add a drop down box with few pre-populated strings in the same?
I could find list box and drop down menu in the HELP document of Python, but not drop down box.

Please help me in this.

Thanks,
BK
Nov 27 '06 #1
Share this Question
Share on Google+
11 Replies


bartonc
Expert 5K+
P: 6,596
Hi,

How do i add a drop down box with few pre-populated strings in the same?
I could find list box and drop down menu in the HELP document of Python, but not drop down box.

Please help me in this.

Thanks,
BK
Tkinter does not have a combobox. If you are using Tkinter, I can supply a module that requires Python Image Library to make a drop-down menu + a text entry + graphics look like a combo box, but it may need some work first. I now use wxPython for GUI.
Nov 27 '06 #2

P: 17
Tkinter does not have a combobox. If you are using Tkinter, I can supply a module that requires Python Image Library to make a drop-down menu + a text entry + graphics look like a combo box, but it may need some work first. I now use wxPython for GUI.

If that is so, could u please help me in this regard. Are there any pre requisites for the issue?

Thanks,
BK
Nov 27 '06 #3

bartonc
Expert 5K+
P: 6,596
If that is so, could u please help me in this regard. Are there any pre requisites for the issue?

Thanks,
BK
I am unclear on which issue you want help with.
Nov 27 '06 #4

P: 17
Hi,

My intention is to have a drop down box (with edit facility) which would have several ip addresses. The user either can select an existing one or else type a new ip.

I could find spinbox option but thats not editable.

So how do i create such a drop down box?

Thanks,
Badri
Nov 28 '06 #5

bartonc
Expert 5K+
P: 6,596
Hi,

My intention is to have a drop down box (with edit facility) which would have several ip addresses. The user either can select an existing one or else type a new ip.

I could find spinbox option but thats not editable.

So how do i create such a drop down box?

Thanks,
Badri
You still have not said which GUI you are using. If you are using Tkinter, I can supply a class, but it will take me some time to make it work for you.
Nov 28 '06 #6

P: 17
You still have not said which GUI you are using. If you are using Tkinter, I can supply a class, but it will take me some time to make it work for you.
Yes, I am using Tkinter.
Nov 28 '06 #7

bartonc
Expert 5K+
P: 6,596
Yes, I am using Tkinter.
Ok, I'll tweek my module to work for you.
Nov 28 '06 #8

P: 1
Would you include the code so everyone could benefit?


Ok, I'll tweek my module to work for you.
Mar 29 '07 #9

bartonc
Expert 5K+
P: 6,596
Would you include the code so everyone could benefit?
Yep. I'll get right on it.
Mar 29 '07 #10

bartonc
Expert 5K+
P: 6,596
Yep. I'll get right on it.
As promised:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. ## I have a working ComboBox which is editable and
  4. ## has a button around here some where. I'll keep
  5. ## looking for that. In the mean time, this Entry
  6. ## subclass shares a StringVar with the menu item
  7. ## that are created form input list(s) to achive a
  8. ## simple ChoiceBox.
  9.  
  10. class ChoiceBox(Entry):
  11.     """ComboBox(parent, itemList=[], *args, kwargs)
  12.        A simple ChoiceBox with checked menu items
  13.        itemList may be a mix of list of strings and lists of tuples of (label, list of strings)
  14.        for one level of sub menu items. *args and kwargs are passed to the Entry widget."""
  15.  
  16.     def __init__(self, parent, itemList=[], *args, **kwargs):
  17.         Entry.__init__(self, parent, *args, **kwargs)
  18.  
  19.         self.pyvar = pyvar = StringVar(self)    # this is the sharing mechanism
  20.         self.config(textvariable=pyvar)         # add the StringVar to self.
  21.  
  22.         self.popup = popup = Menu(self, tearoff=0)
  23.         self.bind("<Button-1>", self.mousedown, add="+")
  24.  
  25.         for item in itemList:
  26.             if type(item) == tuple:
  27.                 submenu = self.GetSubMenu(item[0])
  28.                 for subitem in item[1]:
  29.                     self.AddCBMenuItem(submenu, subitem)
  30.             else:
  31.                 self.AddCBMenuItem(popup, item)
  32.  
  33.     def GetSubMenu(self, label):
  34.         menu = Menu(self, tearoff=0)
  35.         self.popup.add_cascade(menu=menu, label=label)
  36.         return menu
  37.  
  38.     def AddCBMenuItem(self, menu, label):
  39.         menu.add_checkbutton(label=label,
  40.                              command=self.MenuSelect,
  41.                              variable=self.pyvar,   # add the StringVar to a menu.
  42.                              onvalue=label, offvalue='')
  43.  
  44.     def mousedown(self, event):
  45.         x = event.x_root - event.x
  46.         y = event.y_root -  event.y
  47.         self.popup.post(x, y)
  48.         return 'break'
  49.  
  50.     def get(self):
  51.         return self.pyvar.get()
  52.  
  53.     def clear(self):
  54.         self.pyvar.set('')
  55.  
  56.     def MenuSelect(self):
  57.         pass
  58.  
  59.  
  60.  
  61. if __name__ == '__main__':
  62.  
  63.  
  64.  
  65.     def _test():
  66.         root = Tk()
  67. ##        cb = ChoiceBox(root, [('test', ['one', 'two', 'three'])])
  68.         cb = ChoiceBox(root, ['one', 'two', 'three'])
  69.         cb.pack()
  70.         root.mainloop()
  71.  
  72.     _test()
  73.  
Mar 29 '07 #11

P: 1
Cool code, but I don't see how to enable typing in a new entry...?
Apr 3 '15 #12

Post your reply

Sign in to post your reply or Sign up for a free account.