473,573 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tkinter radiobutton

I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
references to them in a 2 dimensional list ( rBtns[r][c] ). It works
fine, and I can even make it so only one button per column can be
selected, by assigning each column to an intVar. In many languages a
radiobutton has a property that can be directly read to see if it is
selected on unselected. Tkinter radiobuttons don't seem to have any
such property. Is there any way to look (via the script not the screen)
to determine if it is selected?, or can this only be achieved via
control variables?

Bill
Jul 19 '05 #1
11 7853
On Sat, 25 Jun 2005 19:34:50 GMT, William Gill <no*****@gcgrou p.net> wrote:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
references to them in a 2 dimensional list ( rBtns[r][c] ). It works
fine, and I can even make it so only one button per column can be
selected, by assigning each column to an intVar. In many languages a
radiobutton has a property that can be directly read to see if it is
selected on unselected. Tkinter radiobuttons don't seem to have any
such property. Is there any way to look (via the script not the screen)
to determine if it is selected?, or can this only be achieved via
control variables?


The value and variable options for a radiobutton seem to be what you're looking for. Here is an example showing how to use them:

----------------------------------------------------------------
from Tkinter import *

root = Tk()
v = StringVar()
Radiobutton(roo t, text='foo', value='foo', variable=v).pac k()
Radiobutton(roo t, text='bar', value='bar', variable=v).pac k()

def p():
print v.get()

Button(root, command=p, text='Print').p ack()

root.mainloop()
----------------------------------------------------------------

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"
Jul 19 '05 #2
>> to determine if it is selected?, or can this only be achieved via
control variables?
The value and variable options for a radiobutton seem to be what
you're looking for.
Thanks, I knew that, but I was looking for a way to read the
selected/unselected status directly. Using control variables gets a
little messy because of the relationship of the rb matrix. They are
arranged in a 4 X 4 matrix where each column is grouped (via intVars) so
that no more than 1 rb per column can be selected, but each row makes up
the 'status' on one 'item' so any combination of buttons in a row is
acceptable.

One solution I have been contemplating requires setting the value of
each rb to the row number ( 0, 1, 2, or 3 or 1,2,3, or 4 in case I need
to use 0 for 'none selected'), and using one intVar for each column.
Then I would have to loop through all four intVars four times to
determine which radiobuttons are selected in each row. That's what I
mean by messy.

Bill

Eric Brunel wrote: On Sat, 25 Jun 2005 19:34:50 GMT, William Gill <no*****@gcgrou p.net> wrote:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
references to them in a 2 dimensional list ( rBtns[r][c] ). It works
fine, and I can even make it so only one button per column can be
selected, by assigning each column to an intVar. In many languages a
radiobutton has a property that can be directly read to see if it is
selected on unselected. Tkinter radiobuttons don't seem to have any
such property. Is there any way to look (via the script not the screen)
to determine if it is selected?, or can this only be achieved via
control variables?

The value and variable options for a radiobutton seem to be what you're
looking for. Here is an example showing how to use them:

----------------------------------------------------------------
from Tkinter import *

root = Tk()
v = StringVar()
Radiobutton(roo t, text='foo', value='foo', variable=v).pac k()
Radiobutton(roo t, text='bar', value='bar', variable=v).pac k()

def p():
print v.get()

Button(root, command=p, text='Print').p ack()

root.mainloop()
----------------------------------------------------------------

HTH

Jul 19 '05 #3
William Gill wrote:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
references to them in a 2 dimensional list ( rBtns[r][c] ). It works
fine, and I can even make it so only one button per column can be
selected, by assigning each column to an intVar. In many languages a
radiobutton has a property that can be directly read to see if it is
selected on unselected. Tkinter radiobuttons don't seem to have any
such property. Is there any way to look (via the script not the screen)
to determine if it is selected?, or can this only be achieved via
control variables?


You can either write a little helper function

def selected(rbn):
return rbn.getvar(rbn["variable"]) == rbn["value"]

or use a custom subclass of Tkinter.Radiobu tton with a 'selected' attribute:

class Radiobutton(Tki nter.Radiobutto n):
def __getattr__(sel f, name):
if name == "selected":
return self.getvar(sel f["variable"]) == self["value"]
raise AttributeError
Peter

Jul 19 '05 #4
> or use a custom subclass ...

I had considered extending radiobutton to add whatever properties
needed, but the net/net is the same, that property must be set using
methods that trigger on the rb command procedure, or an external (to the
rb) control variable value.

The radiobutton widget knows if it is selected or unselected, or it
wouldn't be able to display correctly, but based on what I'm seeing,
that information is inaccessable to the app. Instead the app must
evaluate an associated control variable. That doesn't make sence to me,
but even trying to look at the code for the radiobutton class didn't help.

I guess I need to set up an observer on the control variable, or a
command procedure on the radiobutton (effectively to create my own
control variable).

I know I can 'slice' my original 4 X 4 matrix vertically, by associating
a different intVar to each 'column', but I can't figure out how to
'slice' them horizontally w/o breaking their vertical relationships.
Bill

Peter Otten wrote:
William Gill wrote:

I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
references to them in a 2 dimensional list ( rBtns[r][c] ). It works
fine, and I can even make it so only one button per column can be
selected, by assigning each column to an intVar. In many languages a
radiobutton has a property that can be directly read to see if it is
selected on unselected. Tkinter radiobuttons don't seem to have any
such property. Is there any way to look (via the script not the screen)
to determine if it is selected?, or can this only be achieved via
control variables?

You can either write a little helper function

def selected(rbn):
return rbn.getvar(rbn["variable"]) == rbn["value"]

or use a custom subclass of Tkinter.Radiobu tton with a 'selected' attribute:

class Radiobutton(Tki nter.Radiobutto n):
def __getattr__(sel f, name):
if name == "selected":
return self.getvar(sel f["variable"]) == self["value"]
raise AttributeError
Peter

Jul 19 '05 #5
William Gill wrote:
The radiobutton widget knows if it is selected or unselected, or it
wouldn't be able to display correctly, but based on what I'm seeing,
that information is inaccessable to the app.**Instead*t he*app*must
evaluate an associated control variable.**That *doesn't*make*s ence*to*me,
but even trying to look at the code for the radiobutton class didn't help.


I guessed you wanted to solve a practical problem, but the thoughts
expressed above suggest, err, philosophical qualms. So, for the sake of the
argument and since we both don't know the implementation details, be it in
C or TCL, let's assume that the individual radiobuttons do *not* /know/
whether they are selected or not but instead compare their associated
'variable' with their 'value' every time they are /asked/ to draw
themselves. That would avoid duplicate state and require only log N instead
of N bits. Wouldn't that be an elegant implementation, at least in theory?

So why bother about the layers below when you have all the information to
write code that works?

Peter
Jul 19 '05 #6
I thought the problem was practical, not philosophical, but what do I
know I'm the one asking for help.

I have radiobuttons arranged in 4 rows of 4 buttons each ( 4 rows and 4
columns )

The user can select no more than 1 choice in any column, but any number
in any row.

Restated:
columns can have 0 or 1 selection
rows can have 0,1,2,3, or 4 selections.
Columns can be restricted to 0 or 1 selection through the use of an
intVar. So we now have 4 intVars (control variables)

The app needs to examine the buttons and aggregate the selections for
each row, efectively converting columnar information to row information.

one solution:

Create 4 intVars
Column0 = intVar()
Column1 = intVar()
Column2 = intVar()
Column3 = intVar()

Assign 0, 1, 2, 3 and 4 to values to correspond to the row number.
Row1rb1 = Radiobutton(sel f, variable = Column0, value = 1)
Row1rb2 = Radiobutton(sel f, variable = Column1, value = 1)
Row1rb3 = Radiobutton(sel f, variable = Column2, value = 1)
Row1rb4 = Radiobutton(sel f, variable = Column3, value = 1)
Row2rb1 = Radiobutton(sel f, variable = Column0, value = 2)
Row2rb2 = Radiobutton(sel f, variable = Column1, value = 2)


Row4rb4 = Radiobutton(sel f, variable = Column3, value = 4)

to 'read' the user's response:

Loop through the 4 intVars 4 times; compare their value to the value for
the row being processed; if they are the same bitor a value to a
rowVariable i.e. convert the column information (intVar values) to row
information.
Bill

Peter Otten wrote:
William Gill wrote:

The radiobutton widget knows if it is selected or unselected, or it
wouldn't be able to display correctly, but based on what I'm seeing,
that information is inaccessable to the app. Instead the app must
evaluate an associated control variable. That doesn't make sence to me,
but even trying to look at the code for the radiobutton class didn't help.

I guessed you wanted to solve a practical problem, but the thoughts
expressed above suggest, err, philosophical qualms. So, for the sake of the
argument and since we both don't know the implementation details, be it in
C or TCL, let's assume that the individual radiobuttons do *not* /know/
whether they are selected or not but instead compare their associated
'variable' with their 'value' every time they are /asked/ to draw
themselves. That would avoid duplicate state and require only log N instead
of N bits. Wouldn't that be an elegant implementation, at least in theory?

So why bother about the layers below when you have all the information to
write code that works?

Peter

Jul 19 '05 #7
William Gill wrote:
I thought the problem was practical, not philosophical, but what do I
know I'm the one asking for help.
What follows looks more like a spec than a question.
columns can have 0 or 1 selection
rows can have 0,1,2,3, or 4 selections. Loop through the 4 intVars 4 times; compare their value to the value for
the row being processed; if they are the same bitor a value to a
rowVariable i.e. convert the column information (intVar values) to row
information.


Here's my implementation:

import Tkinter as tk

class Radiogrid(tk.Fr ame):
def __init__(self, master, columns, trace_write=Non e):
tk.Frame.__init __(self)
self.variables = []
self.buttons = []
for x, column in enumerate(colum ns):
var = tk.IntVar()
if trace_write:
var.trace_varia ble("w", trace_write)
self.variables. append(var)
self.buttons.ap pend([])
for y, text in enumerate(colum n):
rbn = tk.Radiobutton( self, text=text, variable=var, value=y)
rbn.grid(column =x, row=y)
self.buttons[-1].append(rbn)
def get_row_state(s elf, row):
return tuple(row == var.get() for var in self.variables)

if __name__ == "__main__":
root = tk.Tk()
def show_state(*arg s):
for i in range(3):
print "row", i, rg.get_row_stat e(i)
print
rg = Radiogrid(root,
["alpha beta gamma".split(),
"one two three".split(),
"guido van rossum".split()],
show_state
)
rg.pack()
root.mainloop()

I hope this will move further discussion from the abstract to the
concrete :-)

Peter

Jul 19 '05 #8
> What follows looks more like a spec than a question.
It is, but I wanted to show why I was getting confused trying to use
control variables to maintain the overall relationship.

Thank you. This is exactly what I'm trying to do, and oddly enough
similar in concept to what I was doing, but far more readable and
maintainable (thus the philosophical component :-))
using 'for x, column in enumerate(colum ns):' for a looping structure
cleared up a lot of the convoluted 'score keeping' I was trying to do in
my nested loops.

Also, does 'row == var.get() for var in self.variables' perform the
comparison row == var.get() for each item in self.variables? I would
have had to write:

for var in self.variables:
return row == var.get()

Again, thanks.

Bill

Peter Otten wrote:
William Gill wrote:

I thought the problem was practical, not philosophical, but what do I
know I'm the one asking for help.

What follows looks more like a spec than a question.

columns can have 0 or 1 selection
rows can have 0,1,2,3, or 4 selections.


Loop through the 4 intVars 4 times; compare their value to the value for
the row being processed; if they are the same bitor a value to a
rowVariable i.e. convert the column information (intVar values) to row
information .

Here's my implementation:

import Tkinter as tk

class Radiogrid(tk.Fr ame):
def __init__(self, master, columns, trace_write=Non e):
tk.Frame.__init __(self)
self.variables = []
self.buttons = []
for x, column in enumerate(colum ns):
var = tk.IntVar()
if trace_write:
var.trace_varia ble("w", trace_write)
self.variables. append(var)
self.buttons.ap pend([])
for y, text in enumerate(colum n):
rbn = tk.Radiobutton( self, text=text, variable=var, value=y)
rbn.grid(column =x, row=y)
self.buttons[-1].append(rbn)
def get_row_state(s elf, row):
return tuple(row == var.get() for var in self.variables)

if __name__ == "__main__":
root = tk.Tk()
def show_state(*arg s):
for i in range(3):
print "row", i, rg.get_row_stat e(i)
print
rg = Radiogrid(root,
["alpha beta gamma".split(),
"one two three".split(),
"guido van rossum".split()],
show_state
)
rg.pack()
root.mainloop()

I hope this will move further discussion from the abstract to the
concrete :-)

Peter

Jul 19 '05 #9
p.s. I tweaked

rbn = tk.Radiobutton( self, text=text, variable=var, value=y)
to
rbn = tk.Radiobutton( self, text=text, variable=var, value=y+1)

and

return tuple(row == var.get() for var in self.variables)
to
return tuple(row+1 == var.get() for var in self.variables)

so that the Radiogrid doesn't initialize w/row 1 selected, and
accomodates cases where nothing is selected in any column.

Bill

Peter Otten wrote:
William Gill wrote:

I thought the problem was practical, not philosophical, but what do I
know I'm the one asking for help.

What follows looks more like a spec than a question.

columns can have 0 or 1 selection
rows can have 0,1,2,3, or 4 selections.


Loop through the 4 intVars 4 times; compare their value to the value for
the row being processed; if they are the same bitor a value to a
rowVariable i.e. convert the column information (intVar values) to row
information .

Here's my implementation:

import Tkinter as tk

class Radiogrid(tk.Fr ame):
def __init__(self, master, columns, trace_write=Non e):
tk.Frame.__init __(self)
self.variables = []
self.buttons = []
for x, column in enumerate(colum ns):
var = tk.IntVar()
if trace_write:
var.trace_varia ble("w", trace_write)
self.variables. append(var)
self.buttons.ap pend([])
for y, text in enumerate(colum n):
rbn = tk.Radiobutton( self, text=text, variable=var, value=y)
rbn.grid(column =x, row=y)
self.buttons[-1].append(rbn)
def get_row_state(s elf, row):
return tuple(row == var.get() for var in self.variables)

if __name__ == "__main__":
root = tk.Tk()
def show_state(*arg s):
for i in range(3):
print "row", i, rg.get_row_stat e(i)
print
rg = Radiogrid(root,
["alpha beta gamma".split(),
"one two three".split(),
"guido van rossum".split()],
show_state
)
rg.pack()
root.mainloop()

I hope this will move further discussion from the abstract to the
concrete :-)

Peter

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4264
by: Mike | last post by:
I've placed a radiobutton on a green panel. The background of the radiobutton is also green. Borderwidth is 0 and relief is FLAT. I still get a thin gray border around the radiobutton. What is this border, and how can I get rid of it. Thanks. Mike
1
2561
by: Bob Greschke | last post by:
Root.option_add("*Menu.Font", "Veranda 9") as an example, works fine in my program. Root.option_add("*Radiobutton*selectColor", "black") also works fine for regular radiobuttons. What I can't do is get the selectColor of the radiobutton's in the menu to be black...the x.add_radiobutton() ones.
6
4471
by: Bob Greschke | last post by:
Root.option_add("*?????*font", "Helvetica 12 bold") Want to get rid of the "font =": Widget.add_cascade(label = "File", menu = Fi, font = "Helvetica 12 bold") Does anyone know what ????? should be to control the font of the cascade menus (the labels on the menu bar)? "*Menu*font" handles the part that drops down, but I can't come up with...
1
3588
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings...
1
2604
by: Ichor | last post by:
This is my code. I've tried a trillion different things. But basically what I want is the radiobuttons to appear. Select one and hit the okay button. Then the screen prints out the var of which button was selected (just to verify). And I want the whole program to wait and do nothing until this process is done. (What I'd want to do next is another...
4
3383
by: joseff | last post by:
Hi evryone!! I' m very new not only to Python and Tkinter but to programing. I'm a bit stuck here and would be grateful for some help. I have 3 radio select buttons(see below) which I want to link to a pmw dialog with (apply and cancel options ) i.e. when I press enable btn dialog pops up and asks to confirm the my choice and if apply is...
8
2013
by: Lie | last post by:
Inspect the following code: --- start of code --- import Tkinter as Tk from Tkconstants import * root = Tk.Tk() e1 = Tk.Entry(root, text = 'Hello World') e2 = Tk.Entry(root, text = 'Hello World')
1
5648
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Beginners Game- Tic-Tac-Toe My first attempt after learning Tkinter from Tkinter import * import tkFont class myApp: """ Defining The Geometry of the GUI And the variables Used In the The methods
3
2291
by: Dream | last post by:
The code create 2 windows. 2 radiobuttons are put on the second window. A control variable "v" is binded to the 2 widgets. But when I run the code, I found the control variable not binded succsessfully: The second radiobutton was not selected by default; Click ed each button always print 1. I don't know what is wrong. So I need help.Thanks. ...
0
7780
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7694
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8021
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8065
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5291
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1303
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.