Home > Software design >  how to check if the user interact with any widgets and execute a function in tkinter
how to check if the user interact with any widgets and execute a function in tkinter

Time:12-17

I'm not a beginner but I don't know how to check if the user interacts with any widgets and execute a function in Tkinter

what I expected is like

root.bind("<interact>", do_something())

edit :

I got the answer

from tkinter import *
from tkinter import ttk

root = Tk()

def printzero(event):
    print(0)

btn1 = Button(text="                    e                        ")
btn1.pack()
btn1 = Button(text="                    e                        ")
btn1.pack()
btn1 = Button(text="                    e                        ")
btn1.pack()

txt1 = Text()
txt1.pack()

root.bind("<Button-1>", printzero)
root.bind("<Button-2>", printzero)
root.bind("<Button-3>", printzero)
root.bind("<Key>", printzero)

root.mainloop()

CodePudding user response:

So The main way I would recommend is to call a function and have that function notify you or carry out whatever desired information you have. In as simple of code as possible I have included.

from tkinter import *

def buttonClicked():
  print("button was clicked")
top = Tk()
send = Button(top, text='Send', command=buttonClicked)
send.pack(side=LEFT)

top.mainloop()

CodePudding user response:

Your code is incorrect. You don't need bind, bind is for other things. I corrected your code

from tkinter import *
from tkinter import ttk

root = Tk()

def printzero():
    txt1.insert(1.0, "0")

btn1 = Button(text="e")
btn1 = Button(root, text="Button 1", bg='#b40909', foreground='white', command=printzero)
btn1.pack()

btn2 = Button(text="e")
btn2 = Button(root, text="Button 1", bg='#b40909', foreground='white', command=printzero)
btn2.pack()

btn3 = Button(text="e")
btn3 = Button(root, text="Button 1", bg='#b40909', foreground='white', command=printzero)
btn3.pack()

txt1 = Text()
txt1.pack()


root.mainloop()
  • Related