Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Making a Label that looks the same as a button.

2 views
Skip to first unread message

Dustan

unread,
Jun 13, 2006, 9:14:03 AM6/13/06
to
I have a Button object that gets replaced by a Label when clicked.

Button(buttonsframe,text=' ',command=c,font=buttonsFont)
Note that the text is a single space. buttonsFont uses 'Courier New' as
a family.

When clicked, the Button is destroyed and replaced with a Label object:
Label(buttonsframe,text=x,font=buttonsFont,relief=RAISED)

The intent is for the Label object to look identical to the button
object, except for the non-space character x. The Label object is a
little smaller than the Button object. When I set borderwidth, the
label object does increase in size, but that's not going to make it
look the same, since it makes the border thicker.

How do I get the Label object to look just like the Button object?

Eric Brunel

unread,
Jun 13, 2006, 9:38:20 AM6/13/06
to

There is least two other options that are different for Labels and Buttons:

>>> b = Button(root)
>>> b.cget('padx')
'3m'
>>> b.cget('pady')
'1m'
>>> l = Label(root)
>>> l.cget('padx')
'1'
>>> l.cget('pady')
'1'

The padx and pady are the distance between the text and the widget
borders. They are larger on a Button.

BTW, you can get all configuration options for any widget with:

for k in widget.configure().keys():
print k, ':', widget.cget(k)

So you can easily compare common options between a standard Label and a
standard Button.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"

Andrew Gwozdziewycz

unread,
Jun 13, 2006, 9:42:02 AM6/13/06
to pytho...@python.org
It's imperative that you explain which toolkit you are using since
they all have differences.

> --
> http://mail.python.org/mailman/listinfo/python-list

---
Andrew Gwozdziewycz
apg...@gmail.com
http://23excuses.com | http://ihadagreatview.org | http://and.rovir.us


Grayson, John

unread,
Jun 13, 2006, 9:49:06 AM6/13/06
to pytho...@python.org


Buttons can look like labels without the need to create another object -
just remove the
Command binding, set state to DISABLED and disabledforeground='same
color as NORMAL'...

This demonstrates how to play with button styles:

import Tkinter as tk

class GUI:
def __init__(self):
self.root = tk.Tk()
self.root.title('Button Styles')
for bdw in range(5):
setattr(self, 'of%d' % bdw, tk.Frame(self.root,
borderwidth=0))
tk.Label(getattr(self, 'of%d' % bdw),
text='borderwidth = %d ' % bdw).pack(side=tk.LEFT)
for relief in [tk.RAISED, tk.SUNKEN, tk.FLAT, tk.RIDGE,
tk.GROOVE, tk.SOLID]:
tk.Button(getattr(self, 'of%d' % bdw), text=relief,
borderwidth=bdw,
relief=relief, width=10,
command=lambda s=self, r=relief, b=bdw:
s.prt(r,b))\
.pack(side=tk.LEFT, padx=7-bdw, pady=7-bdw)
getattr(self, 'of%d' % bdw).pack()

def prt(self, relief, border):
print '%s:%d' % (relief, border)

myGUI = GUI()
myGUI.root.mainloop()


John Grayson

Cameron Laird

unread,
Jun 13, 2006, 11:27:10 AM6/13/06
to
In article <mailman.6967.1150206...@python.org>,

Grayson, John <John.G...@gdc4s.com> wrote:
>
>
>
>Buttons can look like labels without the need to create another object -
>just remove the
>Command binding, set state to DISABLED and disabledforeground='same
>color as NORMAL'...
>
>This demonstrates how to play with button styles:
.
.
.
John, as I read the original poster, Tkinter.DISABLED is *exactly*
what he wants (although he might not realize it yet); I suspect
there's no need at all for other styling.

Are Button and Label styles truly identical except for
disabledforeground? While I can't make the time now to research
this for myself, it surprises me; I thought there were padding
differences ...

Your remark about the Command has me curious: why remove it? In
terms of the original poster's description, what does this serve?
I repeat my speculation that Tkinter.DISABLED will "do it all" for
him.

Fredrik Lundh

unread,
Jun 13, 2006, 12:43:05 PM6/13/06
to pytho...@python.org
Andrew Gwozdziewycz wrote:

> It's imperative that you explain which toolkit you are using since
> they all have differences.

however, if you knew the answer, you would have recognized what toolkit
he was using.

</F>

Dustan

unread,
Jun 13, 2006, 3:25:59 PM6/13/06
to

Yes, that's what I needed (and I knew about that before), but I didn't
know about the disabledforeground option. Thanks for the information;
it worked nicely.

0 new messages