Advertisement
furas

Python - tkinter - listbox set selection

Jun 24th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import tkinter as tk
  4.  
  5. def test():
  6.     # here you can get selected element
  7.     print('previous:', listbox.get('active'))
  8.     print(' current:', listbox.get(listbox.curselection()))
  9.  
  10. # --- main ---
  11.  
  12. root = tk.Tk()
  13.  
  14. listbox = tk.Listbox(root)
  15. listbox.pack()
  16.  
  17. listbox.insert(1, 'Hello 1')
  18. listbox.insert(2, 'Hello 2')
  19. listbox.insert(3, 'Hello 3')
  20. listbox.insert(4, 'Hello 4')
  21. listbox.selection_set(0)
  22.  
  23. button = tk.Button(root, text="Test", command=test)
  24. button.pack()
  25.  
  26. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement