473,386 Members | 1,609 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,386 software developers and data experts.

Python Tkinter - using dynamic names with labels?

75
I have application which creates data table from loaded data, table cells are created using labels.

I am trying to do following: when the mouse cursor is over the one label it will be highlighted using brighter background color

Expand|Select|Wrap|Line Numbers
  1. def mouse_enter(event):
  2.    self.label["bg"] = "#FFFFFF"
  3.  
  4. def mouse_leave(event):
  5.    self.label["bg"] = "#888888"
  6.  
  7. index = 0
  8. while index != 10:
  9.    self.label = Label(frame_x, bg="#888888", text=str(temp[index][0]))
  10.    self.label.place(x=10, y=35 + (index * 25), width=100)
  11.    self.label.bind ("Enter", mouse_enter)
  12.    self.label.bind ("Leave", mouse_leave)
  13.    index += 1
  14.  
so in the code all labels now get the same name, and ofcourse now they all are highlighted (sorry, only the last one) when mouse cursor will be over any label... but how to name these labels dynamically? (ofcourse the amount of labels isn't 10 in my application, it's based on the amount of data needed to display)
Mar 3 '08 #1
1 2159
dazzler
75
solved the problem... not only the dynamic names was problematic but also application needed to send additional info with the event handler...

I'll paste my solution if someone could find this useful ^^


Expand|Select|Wrap|Line Numbers
  1. def mouse_enter(self, event, index):
  2.    d["label_a%s" %index]["bg"] = "#FFFFFF"
  3.    d["label_b%s" %index]["bg"] = "#FFFFFF"
  4.  
  5. def mouse_leave(self, event, index):
  6.    d["label_a%s" %index]["bg"] = "#888888"
  7.    d["label_b%s" %index]["bg"] = "#888888"
  8.  
  9. d = {}  #dictionary
  10.  
  11. index = 0
  12. while index != 10:
  13.  
  14.    def handler_enter (event, self=self, index=index):
  15.       return mouse_enter (self, event, index)
  16.    def handler_leave (event, self=self, index=index):
  17.       return mouse_leave (self, event, index)
  18.  
  19.    d["label_a%s" %index] = Label(frame_x, bg="#888888", text=str(temp[index][0]))
  20.    d["label_a%s" %index].place(x=10, y=35 + (index * 25), width=100)
  21.    d["label_a%s" %index].bind ("Enter", handler_enter)
  22.    d["label_a%s" %index].bind ("Leave", handler_leave)
  23.  
  24.    d["label_b%s" %index] = Label(frame_x, bg="#888888", text=str(temp[index][1]))
  25.    d["label_b%s" %index].place(x=110, y=35 + (index * 25), width=100)
  26.    d["label_b%s" %index].bind ("Enter", handler_enter)
  27.    d["label_b%s" %index].bind ("Leave", handler_leave)
  28.  
  29.    index += 1
  30.  
Mar 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Edilmar | last post by:
Hi, First of all, I'm new in Python... I have worked with manu langs and IDEs, like Delphi, VB, JBuilder, Eclipse, Borland C++, Perl, etc... Then, today I think IDEs like Delphi have a...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
5
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
28
by: H J van Rooyen | last post by:
Hi, I want to write a small system that is transaction based. I want to split the GUI front end data entry away from the file handling and record keeping. Now it seems almost trivially easy...
27
by: bcwhite | last post by:
I've been trying to find out what the future of Python is with regard to Tk. It seems there are several interfaces that make use of new functionality, including "Tile" and "Ttk". If I want to...
59
by: Kevin Walzer | last post by:
From the introduction to PyObjC, the Python-Objective-C bridge on Mac OS X: "As described in Objective-C for PyObjC users the creation of Objective-C objects is a two-stage process. To initialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...

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.