Home python tkinter .bind does not work Get the address

tkinter .bind does not work Get the address

Author

Date

Category

When you hover the mouse on one of the created rectangles, it is not true that it is by

from tkinter import *
TEK_K = 0.
id_k_new = 0.
id_k_posl = 0.
spisok_k = {}
Class Glavn_okno (TK):
  Def __init __ (Self):
    Super () .__ init __ ()
    Self.Geometry ("500x500 + 200 + 0")
    Self.Resizable (0, 0)
    self.canvas = canvas (Self, width = 573, height = 561, highlightthickness = 0)
    self.canvas.place (x = 0, y = 0)
    Self.Button = Button (Self.Canvas, Text = "Click", Command = Self.cikl_knop) .place (x = 400, y = 450)
    Self.Bind ('& lt; Motion & gt;', self.mouse_wheel_knop)
  Def Mouse_Wheel_Knop (Self, Event):
    Global id_k_posl
    Global Spisok_K.
    Global id_k_new.
    Global Tek_K.
    For i in Range (id_k_posl):
      TEK_K = I + 1
      Spisok_k ["knop {}". Format (I + 1)]. Bind ("& lt; Enter & gt;", self.fun_nag)
    # F-I create to
  DEF CIKL_KNOP (SELF):
    Global Spisok_K.
    Global id_k_new.
    Global id_k_posl
    Global Tek_K.
    id_k_new + = 1
    For i in Range (id_k_posl, id_k_new):
      Spisok_K ["knop {}." Format (I + 1)] = canvas (self.canvas, width = 556, height = 46, HighlightThickness = 0)
      Spisok_k ["Knop {}". Format (i + 1)]. Place (X = 50, Y = 50 + 50 * i)
      Spisok_K ["Knop {}". Format (i + 1)]. create_rectangle (0, 0, 150, 60, outline = "# FB0", fill = "# FB0")
      id_k_posl + = 1
  Def Fun_nag (Self, Event):
    Global Tek_K.
    Global id_k_posl
    Print ("Current to", Tek_K)
IF __name__ == "__main__":
  OSN = Glavn_okno ()
  OSN.Mainloop ()

Answer 1, Authority 100%

Several comments to your code:

  1. If you switched to the OOP, do not use GLOBAL – use the object fields
  2. It is not clear why the button is pressed a new canvas. It turns out a bunch of canvases on one rectangle in each. If you just need an analogue of the buttons, but without a “relief” – use just LABEL .
  3. The meaning of the cycle is not clear when the button is pressed. You just need to create one rectangle without a cycle.
  4. Binding handlers to events should be done once – when creating an object, and not in a loop with each movement of the mouse
  5. Give functions and variables of understandable names, do not reduce too much

The following canvas is created below, when you press the button, one rectangle is created, each next is shifted down from the previous one. To bind the event to the figure (rectangle), the canvas method tag_bind is transmitted as a handler, a lambda is transmitted through which the rectangle number is transmitted, which, when the event is triggered, is transmitted already inside the present handler (thus, the “remembers” handler, To which object it is tied).

import tkinter as tk
Class Mainwindow (tk.tk):
  Def __init __ (Self):
    Super () .__ init __ ()
    self.canvas = tk.canvas (SELF)
    self.canvas.pack ()
    TK.BUTTON (Self, Text = "Create a Rectangle", Command = Self.New_Rectangle) .Pack ()
    Self.Rect_counter = 0.
    Self.Rect_y = 0.
  DEF New_Rectangle (Self):
    Rectangle_ID = Self.canvas.create_Rectangle (0, self.Rect_y, 150, self.Rect_y + 60, outline = "# FB0", Fill = "# FB0")
    Self.Rect_counter + = 1
    self.canvas.tag_bind (rectangle_id, '& lt; enter & gt;',
      Lambda Event, Rect_Number = Self.Rect_counter: Self.on_hover (Event, Rect_Number))
    self.Rect_y + = 70 # (rectangle height + 10 so that there was a gap)
  DEF On_hover (Self, Event, Rect_Number):
    Print ("Mouse over Rectangle", Rect_Number)
IF __name__ == "__main__":
  Root = Mainwindow ()
  root.mainloop ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions