Tkinter:已选择所有单选按钮 [英] Tkinter: All radio buttons are already selected
问题描述
这是我第一次使用 tkinter.我在第一个窗口上有一个输入框,我在不同的窗口上创建了单选按钮.问题是在我单击其中任何一个之前它们已经被选中.如何更改要取消选择的所有按钮?
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.
推荐答案
问题是你不应该打开一个新的 Tk()
窗口,而应该创建一个新的 >Toplevel()
窗口,这将解决问题.
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.
所以不要使用 self.__new_window1 = Tk()
使用 self.__new_window1 = Toplevel()
So instead of self.__new_window1 = Tk()
use 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)
这篇关于Tkinter:已选择所有单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!