【发布时间】:2020-12-12 10:27:53
【问题描述】:
我正在尝试为在函数中创建的类对象定义绑定按钮。 因此,我在这里编写简单的代码。当我按下“线路按钮”时,它会在类方法中创建新实例。在“init 方法”中,我正在验证它。但是,当我按右键单击(按钮 3)时,它会出错。它无法访问我在“create_line”函数中创建的类实例。我怎么解决这个问题?我也对其他想法持开放态度,比如在课堂上定义绑定函数?
from tkinter import *
class line_class():
def __init__(self,line_no):
self.line_number=line_no
print(self.line_number)
def settings_menu(self, event):
print(self.line_number, ": line entered")
def create_line():
A=line_class(my_canvas.create_line(200, 200, 100, 100, fill='red', width=5, capstyle=ROUND, joinstyle=ROUND))
root = Tk()
root.title('Moving objects')
root.resizable(width=False, height=False)
root.geometry('1200x600+200+50')
root.configure(bg='light green')
my_canvas = Canvas(root, bg='white', height=500, width=700)
my_canvas.pack()
btn_line = Button(root, text='Line', width=30, command=lambda: create_line())
btn_line.place(relx=0,rely=0.1)
root.bind("<Button-3>",A.settings_menu)
root.mainloop()
【问题讨论】:
标签: python function class tkinter binding