gpt4 book ai didi

python - Tkinter:已选择所有单选按钮

转载 作者:行者123 更新时间:2023-11-28 17:59:54 27 4
gpt4 key购买 nike

这是我第一次使用 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)

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

最佳答案

问题是您不应该打开一个新的 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)

关于python - Tkinter:已选择所有单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56178092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com