0

EDIT: I tried to edit it based on one suggestion that it needs to be tied to a function. But still not working properly. Thank you very much!

Hope you can help me. Saw related questions but not exactly the same. Thank you very much!

I have a radiobutton. Based on the selected value, the number of buttons will change. However, the number of buttons dont change. The number just based on the default value of the radiobutton (i tried changing it).

import tkinter
from tkinter import *

root = Tk()

TIME_SUBFRAME = root


#dont mind much this part, this is just the popup window that will show after I click the buttons dependent on the radiobutton
def month():
    CAL_WINDOW = Toplevel(TIME_SUBFRAME) #other parts removed


#this is the part that I would want to be dependent on the radiobutton
ONLY_MONTH = Button(TIME_SUBFRAME, text='Month', command=month)
START_MONTH = Button(TIME_SUBFRAME, text='Start Month', command=month)
END_MONTH = Button(TIME_SUBFRAME, text='End Month', command=month)

def sing_month():
    START_MONTH.destroy()
    END_MONTH.destroy()
    ONLY_MONTH.grid(row = 3, column = 2, columnspan = 2)

def mult_month():
    ONLY_MONTH.destroy()
    START_MONTH.grid(row = 3, column = 2)
    END_MONTH.grid(row = 3, column = 3)


#Radiobuttons for timepoint selection type
YRTYPE = IntVar(TIME_SUBFRAME, 1) 
SING_MONTH = Radiobutton(TIME_SUBFRAME, text = "Single Month", command = sing_month, variable = YRTYPE, value = 1)
MULT_MONTH = Radiobutton(TIME_SUBFRAME, text = "Multiple Months", command = mult_month, variable = YRTYPE, value = 2)
SING_MONTH.grid(row = 2, column = 2, padx = 10, pady = 10)
MULT_MONTH.grid(row = 2, column = 3, padx = 10, pady = 10)

root.mainloop()
ambmil
  • 115
  • 3
  • 9
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) and get rid of these **UPERCASE** this makes your code hard to read. [when-is-a-variable-considered-constant-in-terms-of-pep8-naming-styles](https://stackoverflow.com/questions/44636868/when-is-a-variable-considered-constant-in-terms-of-pep8-naming-styles) – stovfl Dec 27 '19 at 13:20

1 Answers1

1

The radiobutton numbers needs to checked their value when a function is called.You have declared checking statement in the current running block, which will return initial value of your radiobuttons.

The values needs to be checked when user checks the radiobutton.So, we need an extra button widget commanded with a function that checks the value of both radiobuttons and performs the related actions.

The radiobutton will not check the values automatically, it needs to be checked by function.

Here's your Solution,

import tkinter
from tkinter import *

root = Tk()

TIME_SUBFRAME = root


#dont mind much this part, this is just the popup window that will show after I click the buttons dependent on the radiobutton
def month():
    CAL_WINDOW = Toplevel(TIME_SUBFRAME) #other parts removed


#this is the part that I would want to be dependent on the radiobutton
ONLY_MONTH = Button(TIME_SUBFRAME, text='Month', command=month)
START_MONTH = Button(TIME_SUBFRAME, text='Start Month', command=month)
END_MONTH = Button(TIME_SUBFRAME, text='End Month', command=month)

def sing_month():
        START_MONTH.grid_remove()
        END_MONTH.grid_remove()
        ONLY_MONTH.grid(row = 3, column = 2, columnspan = 2)

def mult_month():
        ONLY_MONTH.grid_remove()
        START_MONTH.grid(row = 3, column = 2)
        END_MONTH.grid(row = 3, column = 3)



#Radiobuttons for timepoint selection type
YRTYPE = IntVar(TIME_SUBFRAME, 1) 
SING_MONTH = Radiobutton(TIME_SUBFRAME, text = "Single Month", command = sing_month, variable = YRTYPE, value = 1)
MULT_MONTH = Radiobutton(TIME_SUBFRAME, text = "Multiple Months", command = mult_month, variable = YRTYPE, value = 2)
SING_MONTH.grid(row = 2, column = 2, padx = 10, pady = 10)
MULT_MONTH.grid(row = 2, column = 3, padx = 10, pady = 10)

root.mainloop()
Divyesh Mehta
  • 191
  • 1
  • 9
  • Hi Divyesh... Thanks for this. Though I have two issues with your answer. First, I would really like to minimize the number of buttons. But most importantly, after I click Next, the Next button is lost and could no longer change my radio button selection. – ambmil Dec 28 '19 at 14:12
  • 1
    I tried to work on your idea that it will not work if not tied up to a function. So, i tried this one but still fail: – ambmil Dec 28 '19 at 14:12
  • 1
    Hi, I recenlty seen your edited question.If you want to save that buttons which have being destroyed, try `widget.grid_remove()`or `widget.forget_grid()` method instead of `widget.destroy()`.This will save your widget and useful when you have to call that widget again.Here I have edited my answer according to your recent question.Thanks. – Divyesh Mehta Dec 28 '19 at 14:45