Tkinter:已选中所有单选按钮

时间:2019-05-17 00:24:31

标签: python user-interface tkinter radio-button

这是我第一次使用tkinter。我在第一个窗口中有一个输入框,并且在其他窗口中创建了单选按钮。问题是在我单击任何一个之前,它们已经被选中。如何更改所有按钮以取消选择?

我不确定我的代码是否正确。

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)

我希望在程序启动时取消所有按钮的选择。

2 个答案:

答案 0 :(得分:2)

问题在于您不应该打开新的Tk()窗口,而应该创建一个新的Toplevel()窗口来解决此问题。

因此,使用self.__new_window1 = Tk()代替self.__new_window1 = Toplevel()

尝试一下。

    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)

答案 1 :(得分:-1)

尝试

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