SHARE
TWEET

Python - tkinter - listbox set selection

furas Jun 24th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top