Setting default option values for Tkinter widgets

H

Harlin Seritt

There are certain options for Tkinter widgets that have default values
that I don't much care for (borderwidth, font come to mind) and
continuously change when I'm building interfaces. With a bit of
tweaking I have been able to give the widgets a look that rivals the
best of them. However, I get tired of doing this when I'm writing code
and would like a way that I could universally change them on my system.
I have tried to find where in the class files (Tkinter.py) that I can
change these and haven't been able to find them. Are there certain
lines in certain files/modules/classes that I can change the values for
these things like font, border, etc.?

Thanks,

Harlin
 
E

Eric Brunel

There are certain options for Tkinter widgets that have default values
that I don't much care for (borderwidth, font come to mind) and
continuously change when I'm building interfaces. With a bit of
tweaking I have been able to give the widgets a look that rivals the
best of them. However, I get tired of doing this when I'm writing code
and would like a way that I could universally change them on my system.
I have tried to find where in the class files (Tkinter.py) that I can
change these and haven't been able to find them. Are there certain
lines in certain files/modules/classes that I can change the values for
these things like font, border, etc.?

tk allows to write your preffered options to an option file and to load it via the method option_readfile in your application.

For an example on how the options can be set, see:
http://mini.net/tcl/10424
The file should contain the arguments passed to "option add" commands.

For a reference on what options are available and how to specify them in an option file, see the tk manual pages here:
http://www.tcl.tk/man/tcl8.4/TkCmd/contents.htm
The name for the widget is usually the name of the corresponding Tkinter class; the name for the option is the second name found in the standard and widget-specific options sections in the man pages (named "Database name" in the section text). So for example, if you want to set the default select color for checkbuttons, you'll put in your option file a line:

*Checkbutton.selectColor: blue

HTH
 
S

Steve Holden

Eric, your tagline doesn't parse correctly on my Python 2.4 shell.
So, are you using Windows?

sholden@bigboy /c/Python24/Lib/site-packages
$ python -c 'print "".join([chr(154 - ord(c)) for c in
"U(17zX(%,5.z5(17l8(%,5.Z*(93-965$l7+-"])'
Eric Brunel (e-mail address removed)

regards
Steve
 
K

Kent Johnson

Steve said:
So, are you using Windows?

If you interchange single and double quotes it works on Windows too
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.z^5(17l8(%,5.Z*(93-965$l7+-'])"

Kent
 
S

stewart.midwinter

yep, that works better under Windows. Pretty obscure use of Python,
though! :)

S
 
E

Eric Pederson

I have

and I want a list of only the unique members.

This seems inefficient, but works fine over my small sample lists:


Is there a more efficient approach for cases where listA is large?






Eric Pederson
:::::::::::::::::::::::::::::::::::
domainNot="@something.com"
domainIs=domainNot.replace("s","z")
ePrefix="".join([chr(ord(x)+1) for x in "do"])
mailMeAt=ePrefix+domainIs
:::::::::::::::::::::::::::::::::::
 
D

Diez B. Roggisch

Eric said:
I have
listA=[1,2,3,4,5,4,3,4,3,2,1]

and I want a list of only the unique members.

This seems inefficient, but works fine over my small sample lists:
listA=[a for a in set(listA)]


Is there a more efficient approach for cases where listA is large?

No. But I doubt that that is what you actually want, as listA will lose its
order afterwards. Typically, something like that gets written like this:

inserted = set()
res = []
for e in listA:
if not e in inserted:
res.append(e)
inserted.add(e)
listA = res

Or, with a little helperfunction:

inserted = set()
def foo(e):
inserted.add(e)
return e
listA = [foo(e) for e in listA if not e in inserted]

But ist's not really much better.
 
M

Max M

Eric said:
I have

listA=[1,2,3,4,5,4,3,4,3,2,1]


and I want a list of only the unique members.

This seems inefficient, but works fine over my small sample lists:

listA=[a for a in set(listA)]

Is there a more efficient approach for cases where listA is large?


no. Even though the code can be a little simpler:

listA = list(Set(listA))

You don't even need to convert it to a list. You can just iterate over
the set.
>>> la = [1,2,3,4,3,2,3,4,5]
>>> from sets import Set
>>> sa = Set(la)
>>> for itm in sa:
.... print itm
1
2
3
4
5

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
R

Roy Smith

Diez B. Roggisch said:
inserted = set()
res = []
for e in listA:
if not e in inserted:
res.append(e)
inserted.add(e)
listA = res

I'm going to go off on a tangent here and put in a plea for better variable
naming. I'm looking at the above code, and can't figure out what "res" is
supposed to be. Is it short for "rest", as in "the rest of the items"?
Residual? Result? Restore? Any of these seems plausable. Not knowing
which makes it much harder to understand what the code does. When you say
"res.append(e)", are you building up the result that you're going to
return, or are you building up a list of elements that got eliminated (i.e.
residual) because they are duplicates?

Yes, I did eventually figure it out. It didn't even take that long, but
this is a trivial little piece of code; it shouldn't have taken any time at
all to figure it out. All you saved by using "res" instead of "result" was
9 keystrokes ("ult", in three places), but it cost every one of your
readers extra brainpower to figure out what you meant. To quote one of
Aahz's better signatures, "Typing is cheap. Thinking is expensive." :)
 
S

Scott David Daniels

Max said:
Eric Pederson wrote:
listA = list(Set(listA))
As of 2.4, set is a built-in type (2.3 had Set in module sets).
Another 2.4-ism is "sorted", which might very well be the way you
want to turn the set into a list:

listA = sorted(set(listA))

for this particular use, you can define sorted in 2.3 as:

def sorted(iterable):
result = list(iterable)
result.sort()
return result

The full sorted functionality can be (and has been) defined in 2.3,
but just using 2.4 would be a better bet.

--Scott David Daniels
(e-mail address removed)
 
D

Diez B. Roggisch

I'm going to go off on a tangent here and put in a plea for better
variable
naming. I'm looking at the above code, and can't figure out what "res" is
supposed to be. Is it short for "rest", as in "the rest of the items"?
Residual? Result? Restore? Any of these seems plausable. Not knowing
which makes it much harder to understand what the code does. When you say
"res.append(e)", are you building up the result that you're going to
return, or are you building up a list of elements that got eliminated
(i.e. residual) because they are duplicates?

Yes, I did eventually figure it out. It didn't even take that long, but
this is a trivial little piece of code; it shouldn't have taken any time
at
all to figure it out. All you saved by using "res" instead of "result"
was 9 keystrokes ("ult", in three places), but it cost every one of your
readers extra brainpower to figure out what you meant. To quote one of
Aahz's better signatures, "Typing is cheap. Thinking is expensive." :)

I usually use much more telling variable names - from the source I just
wrote a minute ago:

self.selection_color_map = {}
self.selection_color = (255,255,0)
self.assigned_color_map = {}
self.default_color = (0,0,0)
self.known_names = sets.Set()

As you can see - no desire to shorten names. But "res" for result is as
hardwired to me as i,j,k for iterating numbers. So bear with me on this
case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top