我正在尝试创建可调整大小的reactangle。一切都很好,因为我添加了frame.pack()来捕获实际的坐标。当我这样做时,画布就消失了。
import tkinter as tk
class NetBuilder(object):
root = None
canvas = None
frame = None
resolution_height = 0
resolution_width = 0
reactangles = [
["statusBar", 40, 50, 60, 70],
]
def on_key_pressed(self, event):
print(event.x)
print(event.y)
def __init__(self):
self.root = tk.Tk()
self.resolution_height = self.root.winfo_screenheight()
self.resolution_width = self.root.winfo_screenwidth()
resolution_for_geometry = str(self.resolution_width) + "x" + str(self.resolution_height)
self.root.geometry(resolution_for_geometry)
self.canvas = tk.Canvas(self.root, width=self.resolution_width, height=self.resolution_height)
self.frame = tk.Frame(self.root,width=self.resolution_width, height=self.resolution_height)
self.frame.bind("<Button-1>", self.on_key_pressed)
self.frame.pack()
def net_creating(self):
for x in self.reactangles:
self.canvas.create_rectangle(x[1], x[2], x[3], x[4])
self.canvas.create_oval(x[1]- 2, x[2] - 2, x[1] + 2, x[2] + 2, outline="#f00", fill="#f00", width=1) #Left-Top-Corner
self.canvas.create_oval(x[3] - 2, x[4] - 2, x[3] + 2, x[4] + 2, outline="#f00", fill="#f00", width=1) #Right-Bottom-Corner
self.canvas.create_oval(x[1] + 2, x[4] + 2, x[1] - 2, x[4] - 2, outline="#f00", fill="#f00", width=1) #Left-Bottom-Corner
self.canvas.create_oval(x[3] - 2, x[2] - 2, x[3] + 2, x[2] + 2, outline="#f00", fill="#f00", width=1)
self.canvas.pack()
if __name__ == '__main__':
x = NetBuilder()
x.net_creating()
x.root.mainloop()
当我不打包画布时,捕获点就可以正常工作。