Tkinter: All radio buttons are already selected

被刻印的时光 ゝ 提交于 2021-02-05 06:05:38

问题


it is my first time using tkinter. I have an entry box on the first window, and I created radio buttons on different windows. The problem is that they are already selected before I click any of them. How can I change all buttons to be deselected?

I am not sure if my codes are right or not.

from tkinter import *
from tkinter import messagebox

class SortingHat:
  # Constructor
  def __init__(self):
    # Create main window
    self.__main_window = Tk()
    self.__main_window.geometry('300x200')
    self.__main_window.title('Sorting Hat')

    bg_image = PhotoImage(file = "HarryPotterlogo1.png")
    bg_label = Label(self.__main_window, image = bg_image, bd=0)
    bg_label.grid(row=1, column=0)
    bg_label.image = bg_image

    self.__first_label = Label(self.__main_window, text = \
                               'Enter Your Name.', fg = 'gold', bg = 'brown4')
    self.__first_label.grid(row=2, column=0)

    # Create Entry box
    self.__entry = Entry(self.__main_window)
    self.__entry.bind('<Return>', self.entry_action)    
    self.__entry.grid(row=3, column=0)

    self.__button = Button(self.__main_window, text = 'Enter', \
                           fg = 'white', bg = 'black', command = self.action)
    self.__button.grid(row=3, column=0, sticky = 'E')

    self.__next_button = Button(text = 'Next', height = 1, width = 10, \
                                fg = 'black', bg = 'white', command = self.next1)
    self.__next_button.grid(row=4, column=0, sticky ='W')

    # Create OK button and Quit button

    self.__quit_button = Button(text='Quit', height = 1, width = 10, \
                                command=self.__main_window.destroy)
    self.__quit_button.grid(row=4, column=0, sticky = 'E')

  def next1(self):
    self.__new_window1 = Tk()
    self.__new_window1.configure(bg = 'brown4')
    self.__new_window1.title('Question 1')    

    self.__second_label = Label(self.__new_window1, text = \
                                'What is your favorite color?  (10 points)', \
                                fg = 'gold', bg = 'brown4')
    self.__second_label.grid(row=0, column=0, sticky = 'W')

    # Question Radiobutton
    self.__rb_var1 = IntVar()

    # Create First question Radiobutton widgets 
    self.__rb1 = Radiobutton(self.__new_window1, text='a. Red and Gold', fg ='red', \
                             bg = 'brown4', variable=self.__rb_var1, value = 1)
    self.__rb2 = Radiobutton(self.__new_window1, text='b. Green and Silver', fg = 'green', \
                             bg = 'brown4', variable=self.__rb_var1 , value = 2)
    self.__rb3 = Radiobutton(self.__new_window1, text='c. Yellow and Black', fg = 'gold', \
                             bg = 'brown4', variable=self.__rb_var1, value = 3)
    self.__rb4 = Radiobutton(self.__new_window1, text='d. Blue and Bronze', fg = 'blue', \
                             bg = 'brown4', variable=self.__rb_var1, value = 4)

    self.__rb1.grid(row=1, column=0)
    self.__rb2.grid(row=2, column=0)
    self.__rb3.grid(row=3, column=0)
    self.__rb4.grid(row=4, column=0)

I want all buttons to be deselected when the program starts.


回答1:


The problem is that you shouldn't be opening a new Tk() window, instead you should be creating a new Toplevel() window, that will fix the issue.

So instead of self.__new_window1 = Tk() use self.__new_window1 = Toplevel()

Try this.

    def next1(self):
        self.__new_window1 = Toplevel()
        self.__new_window1.configure(bg = 'brown4')
        self.__new_window1.title('Question 1')

        # Question Radiobutton
        self.__rb_var1 = IntVar()

        # Create First question Radiobutton widgets
        self.__rb1 = Radiobutton(self.__new_window1, text='a. Red and Gold', fg ='red', bg = 'brown4', variable=self.__rb_var1, value = 1)
        self.__rb2 = Radiobutton(self.__new_window1, text='b. Green and Silver', fg='green', bg='brown4', variable=self.__rb_var1, value=2)
        self.__rb3 = Radiobutton(self.__new_window1, text='c. Yellow and Black', fg='gold', bg='brown4', variable=self.__rb_var1, value=3)
        self.__rb4 = Radiobutton(self.__new_window1, text='d. Blue and Bronze', fg='blue', bg='brown4', variable=self.__rb_var1, value=4)
        self.__rb1.grid(row=1, column=0)
        self.__rb2.grid(row=2, column=0)
        self.__rb3.grid(row=3, column=0)
        self.__rb4.grid(row=4, column=0)



回答2:


Try

self.__rb1.deselect()
self.__rb2.deselect()
self.__rb3.deselect()
self.__rb4.deselect()


来源:https://stackoverflow.com/questions/56178092/tkinter-all-radio-buttons-are-already-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!