fork download
  1. from tkinter import *
  2. from tkinter import ttk
  3. import tkinter.messagebox
  4.  
  5. class App:
  6. def __init__(self):
  7. self.master = Tk()
  8. self.di = {'Asia': ['Japan', 'China', 'Malaysia', 'India', 'Korea',
  9. 'Vietnam', 'Laos', 'Thailand', 'Singapore',
  10. 'Indonesia', 'Taiwan'],
  11. 'Europe': ['Germany', 'France', 'Switzerland'],
  12. 'Africa': ['Nigeria', 'Kenya', 'Ethiopia', 'Ghana',
  13. 'Congo', 'Senegal', 'Guinea', 'Mali', 'Cameroun',
  14. 'Benin', 'Tanzania', 'South Africa', 'Zimbabwe']}
  15. self.variable_a = StringVar()
  16. self.frame_optionmenu = ttk.Frame(self.master)
  17. self.frame_optionmenu.pack()
  18. options = sorted(self.di.keys())
  19. self.optionmenu = ttk.OptionMenu(self.frame_optionmenu, self.variable_a, options[0], *options)
  20.  
  21. self.variable_a.set('Asia')
  22. self.optionmenu.pack()
  23. self.btn = ttk.Button(self.master, text="Submit", width=8, command=self.submit)
  24. self.btn.pack()
  25.  
  26. self.frame_listbox = ttk.Frame(self.master)
  27.  
  28. self.frame_listbox.pack(side=RIGHT, fill=Y)
  29. self.scrollbar = Scrollbar(self.frame_listbox )
  30. self.scrollbar.pack(side=RIGHT, fill=Y)
  31. self.listbox = Listbox(self.frame_listbox, selectmode=SINGLE, yscrollcommand=self.scrollbar.set)
  32. self.variable_a.trace('w', self.updateoptions)
  33.  
  34. self.scrollbar.config(command=self.listbox.yview)
  35. self.listbox.pack()
  36.  
  37. #Populate listbox
  38. for each in self.di[self.variable_a.get()]:
  39. self.listbox.insert(END, each)
  40. self.listbox.select_set(0) #This only sets focus on the first item.
  41. self.listbox.bind("<<ListboxSelect>>", self.OnSelect)
  42.  
  43. self.listbox.selection_set(first=0) #This is supposed to select the first item, but it doesn't appear to do so.
  44. self.master.mainloop()
  45.  
  46. def updateoptions(self, *args):
  47. self.listbox.delete(0, 'end')
  48. for each in self.di[self.variable_a.get()]:
  49. self.listbox.insert(END, each)
  50. self.listbox.select_set(0) #This only sets focus on the first item.
  51. self.listbox.pack()
  52.  
  53. def submit(self, *args):
  54. var = self.variable_a.get()
  55. if messagebox.askokcancel("Selection", "Confirm selection: " + var):
  56. print(var)
  57.  
  58. def OnSelect(self, event):
  59. widget = event.widget
  60. value = widget.get(widget.curselection()[0])
  61. print(value)
  62.  
  63. App()
Runtime error #stdin #stdout #stderr 0.15s 12672KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "/usr/lib/python3.2/tkinter/__init__.py", line 40, in <module>
    import _tkinter
ImportError: No module named _tkinter

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
  File "/usr/lib/python3.2/tkinter/__init__.py", line 42, in <module>
    raise ImportError(str(msg) + ', please install the python-tk package')
ImportError: No module named _tkinter, please install the python-tk package