By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Not able hide frame while selecting default radio button

P: 5
Hii,

There is one application where two radio buttons are available.i.e default and custom. If custom is selected then one panel will be visible which contain available drives list of not selected checkbox which are generated based on list but while i am doing such things it generate checkbox many time while i am selecting custom again and again.It must be generated only once.if i select default radio then panel should be not visible.

How to make panel in tkinter or which widget will be used to hide and show checkbox?
how to generate check button on window properly?

Expand|Select|Wrap|Line Numbers
  1. def drive_select(self):
  2. flag=0
  3. if self.drive_w.get()==2:
  4. flag=1
  5. oc_files = []
  6. root_dir = []
  7. for d in string.ascii_uppercase:
  8. if os.path.exists('%s:\' % d):
  9. root_dir.append('%s:\' % d)
  10.  
  11.     frame3=Frame(self)
  12.     frame3.pack(fill=BOTH)
  13.     for dr in sorted(root_dir):
  14.         chk_drive = Checkbutton(frame3, text= dr, variable= self.drive_w)
  15.         chk_drive.pack(side=RIGHT,padx=5,pady=55)
  16. else:
  17.     "do something"
  18. self.drive_w = IntVar()
  19. self.drive_w.set(1) # Default value i.e. Default drive
  20. sscan =[("Default",1),("Custom",2)]
  21.  
  22. for txt,val in sscan:
  23. radiobtn_scan = Radiobutton(frame2,text=txt,variable=self.drive_w,value=val,command=self.drive_select)
  24. radiobtn_scan.pack(side=LEFT, padx=5, pady=5)
  25.  
Oct 7 '16 #1

✓ answered by dwblas

I'm not sure I understand your delima, but generally you would do something like this simple example
Expand|Select|Wrap|Line Numbers
  1. from functools import partial
  2. from Tkinter import *
  3.  
  4. class CB:
  5.     def __init__(self, master):
  6.       master.geometry("290x250+5+10" )
  7.       self.master = master
  8.  
  9.       self.create_checkbuttons()
  10.       self.create_normal_frame()
  11.       self.create_custom_frame()
  12.       self.exit_button()
  13.  
  14.  
  15. ##-------------------------------------------------------------------
  16.     def cb_handler(self, cb_number, event):
  17.         """ zero is the first checkbutton and one is the 2nd
  18.         """
  19.         print cb_number
  20.         if cb_number==0:  ## normal
  21.             self.nf.grid(row=0, column=2)
  22.             self.cf.grid_forget()
  23.         else:                  ## custom
  24.             self.cf.grid(row=0, column=2)
  25.             self.nf.grid_forget()
  26.  
  27.         self.var_1.set(0)   ## uncheck the buttons
  28.         self.var_2.set(0)
  29.  
  30.     ##-------------------------------------------------------------------
  31.     def create_checkbuttons(self):
  32.         """ each checkbutton will raise it's own frame
  33.         """
  34.         self.cb_frame=Frame(self.master)
  35.  
  36.         ## first checkbutton
  37.         self.var_1 = IntVar()
  38.         self.var_1.set(0)          ## off=default
  39.         b = Checkbutton(self.cb_frame, text="Normal", \
  40.                             font=("courier", 10), variable=self.var_1)
  41.         b.grid(row=0, column=0)
  42.         b.bind ( "<Button-1>", partial(self.cb_handler, 0))  ##<--zero is normal
  43.  
  44.         ## second checkbutton
  45.         self.var_2 = IntVar()
  46.         self.var_2.set(0)          ## off=default
  47.         b2 = Checkbutton(self.cb_frame, text="Custom", \
  48.                             font=("courier", 10), variable=self.var_2)
  49.         b2.grid(row=1, column=0)
  50.         b2.bind ( "<Button-1>", partial(self.cb_handler, 1))  ##<--one is custom
  51.         self.cb_frame.grid(row=0, column=0)
  52.  
  53.     ##-------------------------------------------------------------------
  54.     def create_custom_frame(self):
  55.         self.cf=Frame(self.master)
  56.         Label(self.cf, text="Custom", fg="blue",
  57.               height=10, width=10)).grid()
  58.  
  59.     ##-------------------------------------------------------------------
  60.     def create_normal_frame(self):
  61.         self.nf=Frame(self.master)
  62.         Label(self.nf, text="Normal", fg="red",
  63.               height=10, width=10).grid()
  64.  
  65.     ##-------------------------------------------------------------------
  66.     def exit_button(self):
  67.       exit=  Button(self.master, text='CANCEL',
  68.                    command=self.master.quit, bg='red', fg='white' )
  69.       exit.grid(row=10, column=0, columnspan=2)
  70.  
  71. ##======================================================================
  72. if __name__ == "__main__":
  73.    root = Tk()
  74.    xx=CB(root)
  75.    root.mainloop() 

Share this Question
Share on Google+
5 Replies


Expert 100+
P: 620
Are you using Tkinter (which version-->python3.x or 2x) or are you using Wx, QT, etc.
Oct 7 '16 #2

Expert 100+
P: 620
I'm not sure I understand your delima, but generally you would do something like this simple example
Expand|Select|Wrap|Line Numbers
  1. from functools import partial
  2. from Tkinter import *
  3.  
  4. class CB:
  5.     def __init__(self, master):
  6.       master.geometry("290x250+5+10" )
  7.       self.master = master
  8.  
  9.       self.create_checkbuttons()
  10.       self.create_normal_frame()
  11.       self.create_custom_frame()
  12.       self.exit_button()
  13.  
  14.  
  15. ##-------------------------------------------------------------------
  16.     def cb_handler(self, cb_number, event):
  17.         """ zero is the first checkbutton and one is the 2nd
  18.         """
  19.         print cb_number
  20.         if cb_number==0:  ## normal
  21.             self.nf.grid(row=0, column=2)
  22.             self.cf.grid_forget()
  23.         else:                  ## custom
  24.             self.cf.grid(row=0, column=2)
  25.             self.nf.grid_forget()
  26.  
  27.         self.var_1.set(0)   ## uncheck the buttons
  28.         self.var_2.set(0)
  29.  
  30.     ##-------------------------------------------------------------------
  31.     def create_checkbuttons(self):
  32.         """ each checkbutton will raise it's own frame
  33.         """
  34.         self.cb_frame=Frame(self.master)
  35.  
  36.         ## first checkbutton
  37.         self.var_1 = IntVar()
  38.         self.var_1.set(0)          ## off=default
  39.         b = Checkbutton(self.cb_frame, text="Normal", \
  40.                             font=("courier", 10), variable=self.var_1)
  41.         b.grid(row=0, column=0)
  42.         b.bind ( "<Button-1>", partial(self.cb_handler, 0))  ##<--zero is normal
  43.  
  44.         ## second checkbutton
  45.         self.var_2 = IntVar()
  46.         self.var_2.set(0)          ## off=default
  47.         b2 = Checkbutton(self.cb_frame, text="Custom", \
  48.                             font=("courier", 10), variable=self.var_2)
  49.         b2.grid(row=1, column=0)
  50.         b2.bind ( "<Button-1>", partial(self.cb_handler, 1))  ##<--one is custom
  51.         self.cb_frame.grid(row=0, column=0)
  52.  
  53.     ##-------------------------------------------------------------------
  54.     def create_custom_frame(self):
  55.         self.cf=Frame(self.master)
  56.         Label(self.cf, text="Custom", fg="blue",
  57.               height=10, width=10)).grid()
  58.  
  59.     ##-------------------------------------------------------------------
  60.     def create_normal_frame(self):
  61.         self.nf=Frame(self.master)
  62.         Label(self.nf, text="Normal", fg="red",
  63.               height=10, width=10).grid()
  64.  
  65.     ##-------------------------------------------------------------------
  66.     def exit_button(self):
  67.       exit=  Button(self.master, text='CANCEL',
  68.                    command=self.master.quit, bg='red', fg='white' )
  69.       exit.grid(row=10, column=0, columnspan=2)
  70.  
  71. ##======================================================================
  72. if __name__ == "__main__":
  73.    root = Tk()
  74.    xx=CB(root)
  75.    root.mainloop() 
Oct 7 '16 #3

P: 5
I am using python 2.7 and TKINTER
Oct 10 '16 #4

P: 5
https://www.dropbox.com/s/7be6vc148m...error.PNG?dl=0


Go to the link and see the bugs..
Oct 10 '16 #5

Expert 100+
P: 620
You won't find many on any forum who will click on a blind link. Note that if the program is too large to post it is also too large for a volunteer to wade through. Finally, post what testing you have already done so we can eliminate them.
Oct 10 '16 #6

Post your reply

Sign in to post your reply or Sign up for a free account.