Python 3.x 仅允许在tkinter中选择一个单选按钮
我在Tkinter(Python 3)中有一些单选按钮。这些是Python 3.x 仅允许在tkinter中选择一个单选按钮,python-3.x,tkinter,radio-button,Python 3.x,Tkinter,Radio Button,我在Tkinter(Python 3)中有一些单选按钮。这些是btOrange和btPurple。我需要确保一次只能选择其中一个。我该怎么做 我的代码: from tkinter import * class MyPaint: color = "Black" def __init__(self): window = Tk() self.canvas = Canvas(window, width = 750, height = 500, b
btOrange
和btPurple
。我需要确保一次只能选择其中一个。我该怎么做
我的代码:
from tkinter import *
class MyPaint:
color = "Black"
def __init__(self):
window = Tk()
self.canvas = Canvas(window, width = 750, height = 500, bg = "white")
self.canvas.pack()
colorFrame = Frame(window)
colorFrame.pack()
btOrange = Radiobutton(colorFrame, text = "Orange", bg = "Orange", command = self.setColor("Orange"))
btPurple = Radiobutton(colorFrame, text = "Purple", bg = "Purple", command = self.setColor("Purple"))
通过让两个或多个单选按钮共享一个变量,可以将它们连接在一起。例如:
self.colorVar = StringVar()
btOrange = Radiobutton(..., variable=self.colorVar, value="Orange")
btPurple = Radiobutton(..., variable=self.colorVar, value="Purple")
在这种情况下,self.colorVar
将自动设置为选中的单选按钮