0

I created a simple program with 2 radio buttons, Option-1 and Option-2 respectively.

By default, Option-1 is selected and the label "You selected Option 1" is displayed.

During the runtime, if the user selects Option-2, then the label "You selected Option 2" should be displayed.

With this requirement, I created the program below. But is not working. There is no response after I select Option-2 radio button.

My code is given below.
Can any of you solve my issue ?

import tkinter as tk

class window1:

 def __init__(self, master):
    self.master = master
    self.frame1 = tk.Frame(self.master, width=300, height=100)
    self.frame1.place(x=5, y=5)
    window1.priority = tk.IntVar(value=1)

 def message_radio(self):

    if self.priority.get() == 1:
        Message1 = tk.Label(self.frame1, text="You selected Option 1").place(x=10, y=35)
    else:
       Message2 = tk.Label(self.frame1, text="You selected Option 2").place(x=10, y=35)

 def radio_button(self):
    radiobutton1 = tk.Radiobutton(self.frame1, text="Option 1", variable=self.priority, 
    value=1,command=window1.message_radio(self))
    radiobutton1.place(x=10, y=10)

    radiobutton2 = tk.Radiobutton(self.frame1, text="Option 2", variable=self.priority, 
    value=2,command=window1.message_radio(self))
    radiobutton2.place(x=175, y=10) def main():
def main():   
  root = tk.Tk()
  root.geometry( "300x100" )
  app = window1( root )
  app.radio_button()
  root.mainloop()

if __name__ == '__main__':
 main()

D_00
  • 1,440
  • 2
  • 13
  • 32
Ramesh Kumar
  • 11
  • 1
  • 6
  • Change `window1.message_radio(self)` to `self.message_radio`. – TheLizzard Apr 07 '21 at 16:59
  • Yes, you are very close, But, when the page loads for the first time, the label "You selected Option 1" is not displayed. – Ramesh Kumar Apr 07 '21 at 17:10
  • Just add `self.message_radio()` at the end of the `radio_button` function. – TheLizzard Apr 07 '21 at 17:16
  • Also you know that you create new a `tkinter.Label` widget each time the option is changed. Also when you do `Message1 = tk.Label(...).pack(...)`, `Message1` will always be `None` because the result of the `.pack(...)` method is stored in `Message1` not the `tkinter.Label`. To fix that just separate it in 2 lines like this: `Message1 = tk.Label(...)` then `Message1.pack(...)` – TheLizzard Apr 07 '21 at 17:18
  • Now by default label "You selected Option 1" is displaying. But when I select Option-2 and then when I return to Option-1, both the lables are displaying. – Ramesh Kumar Apr 07 '21 at 17:21
  • Well this question is closed so I can't write an answer. Fix the `window1.message_radio(self)` => `self.message_radio` and ask a new question so I can answer it. – TheLizzard Apr 07 '21 at 17:23
  • I get a message that, I need to wait for 90 min to post my next question. – Ramesh Kumar Apr 07 '21 at 17:39
  • Well I will prepare an answer in the mean time :D – TheLizzard Apr 07 '21 at 17:41
  • Look at [this](https://pastebin.com/mswHJVNA). – TheLizzard Apr 07 '21 at 21:39
  • The solution exactly worked for my requirement. Thanks for your quick response. I'm grateful to you. – Ramesh Kumar Apr 08 '21 at 02:08

0 Answers0