Advertisement
furas

Python - Tkinter - create widget to group widgets

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