Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # https://www.reddit.com/r/learnpython/comments/4uycav/python_and_tkinter_how_to_refresh_gui_upon_button/
- #
- import Tkinter as tk
- import ttk
- import tkMessageBox
- # --- classes ---
- class Combo1(tk.Frame):
- def __init__(self, parent):
- tk.Frame.__init__(self, parent)
- tk.Label(self,
- text = "Choose source(s)."
- ).grid(row=0)
- self.check_var1 = tk.IntVar() # variables lower_case_with_underscore
- self.check_var2 = tk.IntVar() # variables lower_case_with_underscore
- self.check_var3 = tk.IntVar() # variables lower_case_with_underscore
- tk.Checkbutton(self,
- text="1", variable=self.check_var1,
- onvalue=1, offvalue=0, height=1, width=10
- ).grid(row=1)
- tk.Checkbutton(self,
- text="2", variable=self.check_var2,
- onvalue=1, offvalue=0, height=1, width=10
- ).grid(row=2)
- tk.Checkbutton(self,
- text="3", variable=self.check_var3,
- onvalue=1, offvalue=0, height=1, width=10
- ).grid(row=3)
- class Combo2(tk.Frame):
- def __init__(self, parent):
- tk.Frame.__init__(self, parent)
- tk.Label(self,
- text = "Choose Wisely."
- ).grid(row=0)
- self.box_value = tk.StringVar()
- self.box = ttk.Combobox(self, textvariable=self.box_value,
- state='readonly')
- self.box['values'] = ('D', 'E', 'F')
- self.box.current(0)
- self.box.grid(row=1)
- class Application(tk.Tk): # tk.Tk creates main window
- def __init__(self):
- tk.Tk.__init__(self)
- self.title("Thingy")
- self.geometry("500x600")
- self.create_options()
- self.combo1 = Combo1(self) # it creates combo but doesn't show
- self.combo2 = Combo2(self) # it creates combo but doesn't show
- def create_options(self):
- '''create and show options'''
- self.widgets = tk.Frame(self)
- tk.Label(self.widgets,
- text="Choose an option."
- ).grid(row=0, column=0)
- tk.Label(self.widgets,
- text="Select one:"
- ).grid(row=1, column=0)
- self.favorite = tk.IntVar()
- tk.Radiobutton(self.widgets,
- text="Option1", variable=self.favorite,
- value=1,
- command=self.show_combo1 # show_combo1
- ).grid(row=2, column=0)
- tk.Radiobutton(self.widgets,
- text="Option2", variable=self.favorite,
- value=2,
- command=self.show_combo2 # show_combo2
- ).grid(row=3, column=0)
- self.widgets.grid(column=0)
- def show_combo1(self):
- '''hide combo2 and show combo1'''
- self.combo2.grid_forget()
- self.combo1.grid(row=0, column=1)
- def show_combo2(self):
- '''hide combo1 and show combo2'''
- self.combo1.grid_forget()
- self.combo2.grid(row=0, column=1)
- # --- start ---
- Application().mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement