from Tkinter import *
class Application(Frame):
def setcolor(self):
self["bg"] = "blue"
def createWidgets(self):
self.b1 = Button(self, bg = "red", command = self.setcolor)
self.b1.place(height = 50, width = 50)
self.b2 = Button(self, text = "Exit", command = self.quit)
self.b2.place(height = 50, width = 50, x = 50)
def __init__(self, master=None):
Frame.__init__(self, master)
self.place(height = 50, width = 100)
self.createWidgets()
app = Application()
app.mainloop()
1) When I run this program, it displays two buttons. When I click the
button on the left, I would like the color of that button to change from red
to blue. This code is obviously the wrong way to accomplish this, because
when setcolor is called, it gets the button's parent, not the button itself.
How do I arrange for setcolor to get the right object?
2) The window in which these buttons appear is the wrong size, and does not
depend on the height and width given to self.place in __init__. Yet the
height and width arguments do something, because if I set width to 75, it
cuts off half the right-hand button. How do I say how large I want the
window to be? 5 1951
"Andrew Koenig" <ar*@acm.org> writes: 1) When I run this program, it displays two buttons. When I click the button on the left, I would like the color of that button to change from red to blue. This code is obviously the wrong way to accomplish this, because when setcolor is called, it gets the button's parent, not the button itself. How do I arrange for setcolor to get the right object?
In the specific example, you could just *know* that setcolor deals
with self.b1. In the more general example, you can create dynamic
callback functions:
self.b1 = Button(self, bg = "red",
command = lambda: self.b1.config(bg="blue"))
This uses a number of tricks: the lambda function has no arguments,
yet it uses self - so it is a nested function. Also, inside a lambda
function, you can have only expressions, so self.b1['bg']='blue' would
not be allowed. In the general case, and not assuming nested
functions, you would write
def createWidgets(self):
def b1_setcolor(self=self):
self.b1['bg']='blue'
self.b1 = Button(self, bg = "red", command=b1_setcolor)
2) The window in which these buttons appear is the wrong size, and does not depend on the height and width given to self.place in __init__. Yet the height and width arguments do something, because if I set width to 75, it cuts off half the right-hand button. How do I say how large I want the window to be?
The problem is that there is another toplevel widget around your
frame; the frame itself has the size you have specified. You could
either use Toplevel instead of Frame as a base, or you could adjust
the size of the root window, e.g. through
app.master.wm_geometry("100x50")
HTH,
Martin
> In the specific example, you could just *know* that setcolor deals with self.b1. In the more general example, you can create dynamic callback functions:
self.b1 = Button(self, bg = "red", command = lambda: self.b1.config(bg="blue"))
This uses a number of tricks: the lambda function has no arguments, yet it uses self - so it is a nested function. Also, inside a lambda function, you can have only expressions, so self.b1['bg']='blue' would not be allowed. In the general case, and not assuming nested functions, you would write
def createWidgets(self): def b1_setcolor(self=self): self.b1['bg']='blue' self.b1 = Button(self, bg = "red", command=b1_setcolor)
I worked out something similar, but I must confess that it appears
needlessly complicated. I was hoping for a simpler solution, such as a
variation of the "command" attribute that would cause its associated
argument to be called with the button rather than its parent.
Your first suggestion, knowing that setcolor deals with self.b1, doesn't
work with my application because I'm going to have lots of these buttons,
and I want to be able to set their colors independently. 2) The window in which these buttons appear is the wrong size, and does
not depend on the height and width given to self.place in __init__. Yet the height and width arguments do something, because if I set width to 75,
it cuts off half the right-hand button. How do I say how large I want the window to be?
The problem is that there is another toplevel widget around your frame; the frame itself has the size you have specified. You could either use Toplevel instead of Frame as a base, or you could adjust the size of the root window, e.g. through
app.master.wm_geometry("100x50")
Gotcha -- thanks.
Andrew Koenig wrote: work with my application because I'm going to have lots of these buttons, and I want to be able to set their colors independently.
If you have many buttons with similar functionality, I'd suggest using a
subclass, e. g.:
import Tkinter as tk
class ColorButton(tk.Button):
def __init__(self, master, text, color):
tk.Button.__init__(self, master, text=text, command=self.execute)
self.color = color
def execute(self):
self["background"] = self.color
root = tk.Tk()
for color in "red green blue yellow".split():
ColorButton(root, text=color.capitalize(), color=color).pack()
root.mainloop()
Peter
On Sun, 02 Nov 2003 21:32:12 GMT, "Andrew Koenig" <ar*@acm.org>
wrote: Your first suggestion, knowing that setcolor deals with self.b1, doesn't work with my application because I'm going to have lots of these buttons, and I want to be able to set their colors independently.
A slight variant on the technique using lambda is:
def setcolor(self,widget=self):
widget['bg']='blue'
def createWidgets(self):
self.b1 = Button(self, bg = "red",
command=lambda: self.setcolor(self.b1))
Here we use the lambda to call the setcolor method with
the widget parameter and use that within the setcolor method.
This way you keep one method but call it from several places.
The downside is you introduce an extra function call, but in
a GUI event handler that's not going to be a problem!
HTH,
Alan G.
Author of the Learn to Program website http://www.freenetpages.co.uk/hp/alan.gauld
On Sun, 2003-11-02 at 21:32, Andrew Koenig wrote: In the specific example, you could just *know* that setcolor deals with self.b1. In the more general example, you can create dynamic callback functions:
self.b1 = Button(self, bg = "red", command = lambda: self.b1.config(bg="blue"))
This uses a number of tricks: the lambda function has no arguments, yet it uses self - so it is a nested function. Also, inside a lambda function, you can have only expressions, so self.b1['bg']='blue' would not be allowed. In the general case, and not assuming nested functions, you would write
def createWidgets(self): def b1_setcolor(self=self): self.b1['bg']='blue' self.b1 = Button(self, bg = "red", command=b1_setcolor)
I worked out something similar, but I must confess that it appears needlessly complicated. I was hoping for a simpler solution, such as a variation of the "command" attribute that would cause its associated argument to be called with the button rather than its parent.
Your first suggestion, knowing that setcolor deals with self.b1, doesn't work with my application because I'm going to have lots of these buttons, and I want to be able to set their colors independently.
For this I would use a callback class and it's __call__ method
(untested)
class Callback:
def __init__(self, button, colour):
self.button = button
self.colour = colour
def __call__(self):
self.button.config(background = self.colour)
self.b1 = Button(self, background = "red")
self.b1.config(command = Callback(self.b1, "blue"))
self.b1.pack(.....)
Regards
Martin
--
Martin Franklin <mf********@gatwick.westerngeco.slb.com> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark 'Kamikaze' Hughes |
last post by:
In the new Python game I'm developing, I need to crop out individual
tiles from larger tilesets, and maintain transparency. Unfortunately,
I've run into major deficiencies in both Tkinter and PIL...
|
by: Mickel Grönroos |
last post by:
Hi everybody,
I'm using QuickTimeTcl (3.1) to be able to play movie files in my Tkinter
application (Python 2.3.2) on Windows 2000. I was planning to write a
simple wrapper class,...
|
by: import newbie |
last post by:
Hi all,
I'm a programming dabbler trying learn Python, and I've got a few
questions.
Mainly: Where can I find a good open-source library or tutorial
(preferably free) that explains how to...
|
by: John Chambers |
last post by:
Sp my latest adventure is attempting to use python's Tkinter module on a few machines.
On my PB (OSX 10.3.9), I got the following confusing results:
/Users/jc: python
Python 2.3 (#1, Sep 13...
|
by: Franz Steinhaeusler |
last post by:
Hello NG,
I'm asking this, (although I know a mailing list on gmane
gmane.comp.python.tkinter and there is so little traffic
compared to the mailing list of wxPython also mirrored
on gmane...
|
by: pragy |
last post by:
Hey, can any one help me for writing a program of naive gauss
elimintaion technique?
It's a technique to solve system of simultaneous linear equations
using matrix.
thanks
|
by: Dick Moores |
last post by:
In a couple of places recently I've seen Brent Welch's _Practical
Programming in Tcl & Tk_ (<http://tinyurl.com/ynlk8b>) recommended
for learning Tkinter well.
So a couple of questions:
1) Is...
|
by: fabdeb |
last post by:
Hi every one,
I m a sysadmin who want to know how to use python.
I dont know anything about oriented object programation, i only know
bash and a little perl.
I have some simple questions about...
|
by: Davy |
last post by:
Hi all,
I have written a simple Tkinter program, that is draw a rectangle in a
canvas, when I press Up key, the rectangle move up. But the program
seems work not properly? My environment is...
|
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: 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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |