473,769 Members | 6,473 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 7873
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
4353
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
2587
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
4489
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 the menu bar labels part. Thanks!
1
3598
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 Panel. I just want it to bring up one. Is there an easy way to do that?
1
2613
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 radiobutton set, but I'm not there yet.) Help! I've been researching this online all day today....
4
3394
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 selected then run my enable script if cancel then close the dialog. the same with the other radio...
8
2026
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
5664
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
2296
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. from Tkinter import * def test(event_instance):
0
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.