473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Subclassing Tkinter Buttons

I don't use classes much (mainly because I'm stupid), but I'd like to make a
subclass of the regular Tkinter Button widget that simply adds spaces to the
passed "text=" when the program is running on Windows (Linux, Solaris, Mac
add a little space between the button text and the right and left edges of a
button, Windows does not and it looks bad/can be hard to read). Below is
some pseudo code. What should the guts of the BButton class be? I can't
work out how all of the arguments that would be passed to a regular Button
call get handled. *args and **kw confuse me and I can't seem to find simple
enough examples in my mountain of books.

Thanks!

Bob
System = platform[:3].lower()
..
..
class BButton(Button):
if System == "win":
Make a button with " " before and after text
else:
Make a button using the passed text as is
..
..
BButton(Sub, text = "Hello", bg = "blue", fg = "yellow").pack(side = TOP)
BButton(Sub, bg = "yellow", bg = "blue", text = "World").pack(side = TOP)
On "lin", "sun", "dar":
[Hello]
[World]

On "win":
[ Hello ]
[ World ]
Aug 31 '06 #1
1 2529

Bob Greschke wrote:
I don't use classes much (mainly because I'm stupid), but I'd like to make a
subclass of the regular Tkinter Button widget that simply adds spaces to the
passed "text=" when the program is running on Windows (Linux, Solaris, Mac
add a little space between the button text and the right and left edges of a
button, Windows does not and it looks bad/can be hard to read). Below is
some pseudo code. What should the guts of the BButton class be? I can't
work out how all of the arguments that would be passed to a regular Button
call get handled. *args and **kw confuse me and I can't seem to find simple
enough examples in my mountain of books.
So watch this:

<code>
import Tkinter as Tk
import sys

class MyButton(Tk.Button):
system = sys.platform[:3] # 1

def __init__(self, master=None, **kw): # 2 3
if self.system == "win":
kw["padx"] = kw.get("padx", 0) + 5 # 4
# alternative solution
# if "text" in kw and self.system == "win":
# kw["text"] = " " + kw["text"] + " "

Tk.Button.__init__(self, master, **kw) # 5
</code>

1. system is a class attribute, so is shared by the class and all
instances of the class
2. __init__ is called immediately after an instance of the class
is created
3. kw is a dictionary of button options for example:
{"text": "Hello", "padx": 2}
4. change value of element "padx" or add this element to dictionary kw
5. call parent __init__ method to create button

HTH,
Rob

Sep 1 '06 #2

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

Similar topics

8
by: Jay | last post by:
I'm having a problem using lambda to use a command with an argument for a button in Tkinter. buttons = range(5) for x in xrange(5): buttons = Button(frame, text=str(x+1), command=lambda:...
6
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
1
by: viv1tyagi | last post by:
Hi everyone ! ! ! I'm just a month old in the world of Python and trying to develop an application using Tkinter in which a new window pops out on a particular button press.The code for this new...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.