Hello ,

I am asking about how to create a cickable Area in a Tkinter windows , or if i can make a label Clickable ? this is how the labels are defined :

for z in rows:
                distligne = 15 + distligne
                a = (z[0].strip(),z[1].strip(),z[2].strip())
                print z[0]
                print a
                list.append(a)
                label = Label(interf0, text="%s           %s           %s"%(z[2], z[0],z[1]), justify=LEFT, bg = "blue", fg = "black" ).place(x=40 , y=120+ distligne)

Recommended Answers

All 17 Replies

Thank you pyTony , but i can't use Classes , i try to define a cliquable area in my close to my label .

I would suggest that you use a button instead of a label, which can execute a funtion when it is clicked. Also, the name "list" is already used by Python so you should not override it, although you never use the variable "list" in the code posted so the "list" code can just be deleted.

Thank you pyTony , but i can't use Classes , i try to define a cliquable area in my close to my label.

If you don't mind me asking, why can't you use a class? Is it a case where you can't (i.e., you aren't familiar with developing them), or where you aren't permitted to? It seems like a strange requirement, which is why I am asking about it.

Hello Schol-R-LEA

the reason is as u said ; i'm not familliar with using classes , even i like the idea of them because the resume things , but i never used , i didn't have chance to start with , the is it , :) .

Hmmn, OK, so that's understandable. I would recommend that you try to take some time out to learn about them, however, if at all possible. Not only are classes and objects powerful tools, they are key concepts in Python, and heavily used in TKInter. In Python, all of the datatypes - numbers, strings, lists, tuples, dictionaries, etc. - are actually classes, and all the types defined by TKInter (e.g., Labels, Buttons, etc.) are as well. TKInter really expects you to define sub-classes to inherit from certain their classes (e.g., Frame) in order to make use of them.

I see , i understand you , thank you very much for the explanation , and i try to bigin with classes as soon as i can . Actualy , i did bigin already to work in a small personnal project ; about a small programm that can connect a database , print , edit datas , count , add datas , like a inventory management ... If you like we can share this project , and discuss about it , thank you again ?

Certainly, I'd be willing to help out any way I could (short of doing the work for you, of course).

Thank you very much , if you can help me about having a hand mouse when it's on the "Label" , now when i click that "label" it call the function , this is it's code :

def callback(event):
        print "clicked at", event.x, event.y
        Interface1()

    lab1dep = Label(interf1, text="Departement Informatique" ,bg = "blue" ,font=("times",12, "bold"),  fg = "white" )
    lab1dep.place ( x=40 , y=220)

    lab1dep.bind("<Button-1>", callback)

I'm not sure I understand; where is the issue? Is it that you want to switch the mouse pointer when it is over the button, or is there an issue with the callback() function you currently have?

If want you want is to have the cursor change to the hand cursor when you mouse over the label, then the following should work:

from tkinter import Tk, Frame, Label


def label1_lclick(event):
    print ("clicked at {},{}".format(event.x, event.y))

def label1_mouseover(event):
    master.config(cursor="hand2")

def label1_mouseexit(event):
    master.config(cursor='')


if __name__ == "__main__":
    master = Tk()


    mainFrame = Frame(master, height=500, width=300)

    label1dep = Label(mainFrame, text="Departement Informatique" ,bg = "blue" ,font=("times",12, "bold"),  fg = "white" )
    label1dep.place (x=40 , y=220)

    label1dep.bind("<Button-1>", label1_lclick)
    label1dep.bind("<Enter>", label1_mouseover)
    label1dep.bind("<Leave>", label1_mouseexit)

    mainFrame.pack()

    master.mainloop()

Let me know if that's what you wanted or not.

Hello again ,

Oh , you used classes ;) , i try to understand this code , and try it , then i tell you the result , thank you again very much .

that is working very nice :) , i try now to insert this methode inside the code i made .

Oh , you used classes ;)

Er, only in the technical sense that TkInter is all built around classes. I didn't define any class of my own here.

Yes , I understood , u just used it , that afraid me at first , now it works very nice with all labels , i tell you where i'm blocked when i am :) .

Perhaps you could use some additional expalantions on variables, objects, functions, methods and classes.

An object is a set of properties and the set of actions which can be applied to those properties. For example, 17 is an object, representing the number seventeen. The string 'hello, world!' is also an object. The list [23] is an object that contains another object, and has the properties of length (one) and contents (23). A TkInter Label is an object with several properties, such as it's text, it's size, and it's position on the Frame. The properties of compound objects such as a Label are themselves objects.

A variable is a reference to (that is to say, a name for) an object. All variables in Python refer to objects. A variable can be changed so that it refers to a different object; for example, you could assign fnord to have a value of 5, then reassign it to a value of 17. The object 5 is still in memory though, and will remain in memory until all references to it are gone.

A function is a special kind of variable that represents a series of actions. A function can take a series of parameters, which are variables which, when the function is called, or run, are bound to the actual arguments of the function. A function can also return a value. An example of a function would be:

def foo(a, b):
    return a * b

which is then called like so:

x = foo(3, 4)  # x now refers to the object '12'

An instance method is a function which is bound to an object. It is defined as part of the object's interface, the view the outside world has of the object. An instance method is always called with the name of the object it acts upon:

bar.add(1)

a class is a description of a category of objects. It defines the properties and methods of the objects of that type. A simple example of a class would be:

class Vehicle (Object):
    def __init__(self, max_speed):
        self.max_speed = max_speed
        self.speed = 0

    def move(self, speed):
        if speed < max_speed:
            self.speed = speed
        else:
            self.speed = max_speed

The self variable is a special reference to the object itself; it is used to refer to the properties of the object from inside the class.

A class can have its own class variables and class methods, which are shared between all the objects of the class. These are referred to with the name of the class, rather than the name of the individual object.

The methods which begin and end with two underscores are special methods, which refer to operators or global functions such as len() or str(). On of the special methods, __init__(), is called the class's constructor, or c'tor. The purpose of the c'tor is to create new objects of that class. It is invoked with the name of the class alone:

myVehicle = Vehicle(200)
myVehicle.move(15)

Note that the self variable is implicit, not explicit, in this call.

A class can be a sub-class of another class, for example, we can define a class Car that is a subclass of class Vehicle:

class Car (Vehicle):
    def __init__(self, max_speed, price):
        Vehicle.__init__(self, max_speed)
        self.price = price

This class extends the Vehicle class by adding a sales price for the Car. It inherits the move() method, so you can use it the same way you would with a Vehicle object:

myCar = Car(55, 25000)
myCar.move(45)

OK, that should be enough for now, I hope I haven't confused you too much.

Hello :)

Don't worry , this is a very nice explanation , it's very clear ur examples , thank you so much , i think i will bigin using classes in the future . Can i use classes inside a normal programme ?
I like if you can guide me in my actual project , we can discuss about the easy way to do things , because maybe for easy functiions , or things that will not need many children objects , it's ok to not use classes .
I explain to you what i'm trying to do actualy ; i wanted to make a small programm that can be useful for managing things ( store , persons , actions ... )
i'm sure when you will see the code , you will lauph :) :
_the first screen : is about authenfication
_the secend screen : there is clickable labels ( about this i was asking at first here ) , i have now one function for all the 3 labels that send to an other screen .
_the third one : i want to print datas from databse ( about the computer parc ) , and could insert datas from entries , and idit them :)

Now i'm working in how to add datas from this screen , and the best way to edit data , and also a best way to print them , actualy i print datas on the frame using labels , i like labels :) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.