Reputation: 1343
I have some RadioButtons in Tkinter (Python 3). These are btOrange
and btPurple
. I need to make sure that only one of them can be selected at a time. How can I do this?
My code:
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"))
Upvotes: 1
Views: 2144
Reputation: 386362
You tie two or more radio buttons together by having them share a variable. For example:
self.colorVar = StringVar()
btOrange = Radiobutton(..., variable=self.colorVar, value="Orange")
btPurple = Radiobutton(..., variable=self.colorVar, value="Purple")
In this case, self.colorVar
will be automatically set to whichever radio button is selected.
Upvotes: 1