Advertisement
furas

Python - Tkinter - using frames to group widgets

Jul 29th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. #
  2. # https://www.reddit.com/r/learnpython/comments/4uycav/python_and_tkinter_how_to_refresh_gui_upon_button/
  3. #
  4.  
  5. import Tkinter as tk
  6. import ttk
  7. import tkMessageBox
  8.  
  9. # --- classes ---
  10.  
  11. class Application(tk.Tk): # tk.Tk creates main window
  12.  
  13.     def __init__(self,):
  14.         tk.Tk.__init__(self)
  15.  
  16.         self.title("Thingy")
  17.         self.geometry("500x600")
  18.        
  19.         self.create_options()
  20.         self.create_combo1() # it creates combo but doesn't show
  21.         self.create_combo2() # it creates combo but doesn't show
  22.  
  23.     def create_options(self):
  24.         '''create and show options'''
  25.        
  26.         self.widgets = tk.Frame(self)
  27.        
  28.         tk.Label(self.widgets,
  29.               text="Choose an option."
  30.               ).grid(row=0, column=0)
  31.         tk.Label(self.widgets,
  32.               text="Select one:"
  33.               ).grid(row=1, column=0)
  34.  
  35.         self.favorite = tk.IntVar()
  36.  
  37.         tk.Radiobutton(self.widgets,
  38.                     text="Option1", variable=self.favorite,
  39.                     value=1,
  40.                     command=self.show_combo1 # show_combo1
  41.                     ).grid(row=2, column=0)
  42.         tk.Radiobutton(self.widgets,
  43.                     text="Option2", variable=self.favorite,
  44.                     value=2,
  45.                     command=self.show_combo2 # show_combo2
  46.                     ).grid(row=3, column=0)
  47.  
  48.         self.widgets.grid(column=0)
  49.  
  50.        
  51.     def create_combo1(self):
  52.         '''create combo1 but don't show it'''
  53.  
  54.         self.combo1 = tk.Frame(self) # parent for other elements
  55.        
  56.         tk.Label(self.combo1,
  57.             text = "Choose source(s)."
  58.             ).grid(row=0)
  59.            
  60.         self.check_var1 = tk.IntVar() # variables lower_case_with_underscore
  61.         self.check_var2 = tk.IntVar() # variables lower_case_with_underscore
  62.         self.check_var3 = tk.IntVar() # variables lower_case_with_underscore
  63.        
  64.         tk.Checkbutton(self.combo1,
  65.                 text="1", variable=self.check_var1,
  66.                 onvalue=1, offvalue=0, height=1, width=10
  67.                 ).grid(row=1)
  68.                
  69.         tk.Checkbutton(self.combo1,
  70.                 text="2", variable=self.check_var2,
  71.                 onvalue=1, offvalue=0, height=1, width=10
  72.                 ).grid(row=2)
  73.                
  74.         tk.Checkbutton(self.combo1,
  75.                 text="3", variable=self.check_var3,
  76.                 onvalue=1, offvalue=0, height=1, width=10
  77.                 ).grid(row=3)
  78.  
  79.  
  80.     def create_combo2(self):
  81.         '''create combo2 but don't show it'''
  82.        
  83.         self.combo2 = tk.Frame(self) # parent for other elements
  84.        
  85.         tk.Label(self.combo2,
  86.                 text = "Choose Wisely."
  87.                 ).grid(row=0)
  88.  
  89.         self.box_value = tk.StringVar()
  90.        
  91.         self.box = ttk.Combobox(self.combo2, textvariable=self.box_value,
  92.                                 state='readonly')
  93.                                
  94.         self.box['values'] = ('D', 'E', 'F')
  95.         self.box.current(0)
  96.         self.box.grid(row=1)
  97.  
  98.  
  99.     def show_combo1(self):
  100.         '''hide combo2 and show combo1'''
  101.  
  102.         self.combo2.grid_forget()
  103.         self.combo1.grid(row=0, column=1)
  104.  
  105.  
  106.     def show_combo2(self):
  107.         '''hide combo1 and show combo2'''
  108.  
  109.         self.combo1.grid_forget()
  110.         self.combo2.grid(row=0, column=1)
  111.        
  112.  
  113. # --- start ---
  114.  
  115. Application().mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement