绘制矩形 tkinter 无法正常工作

draw rectangle tkinter not functioning

下面是通过单击拖动和释放来绘制矩形的代码...由于某种原因它不起作用,canvas 屏幕显示但矩形没有被绘制?是 root.mainloop() 行吗?因为我改变了它,因为我需要画弧线和线,不能只有 app = rectangle 和 app.mainloop...对不起,我真的很陌生。

from tkinter import Canvas, Tk, mainloop
import tkinter as tk 
from PIL import Image, ImageTk

# Image dimensions
w,h = 800,400

# Create canvas
root = Tk()
canvas = Canvas(root, width = w, height = h, bg='#D2B48C', cursor = "cross")
canvas.pack()


class Rectangle(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.x = self.y = 0
        self.canvas.pack(side="top", fill = "both", expand = True)
        self.canvas.bind("<ButtonPress-1>", self.press)
        self.canvas.bind("<B1-Motion>", self.move)
        self.canvas.bind("<ButtonRelease-1>", self.release)
        self.rect = None
        self.start_x = None
        self.start_y = None

    def press(self, event):
        # save mouse drag start position
        self.start_x = event.x
        self.start_y = event.y
        self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill="red")

    def move(self, event):
        mouseX, mouseY = (event.x, event.y)

        # expand rectangle as you drag the mouse
        self.canvas.coords(self.rect, self.start_x, self.start_y, mouseX, mouseY)

    def release(self, event):
        pass

# Other Classes for arc and pencil begin here

root.mainloop()

谢谢大家!!!

首先,您永远不会创建 Rectangle class 的实例,这是您所有逻辑所在的地方。

另外,您必须只有一个 Tk 实例。您在问题开始时创建一个,然后如果您创建 Rectangle 的实例,您将创建另一个。同样,你只能拥有一个。

作为@BryanOakley的评论,提供的源包含未完成的分析并揭示了对 tkinter 使用的误解。

问题 1 - class Rectangle 不需要继承自 tk.Tk

Instead of inheriting the class Rectangle from tk.Tk, add the Canvas instance at instance creation.

 class Rectangle(): # not inherit from ==> tk.Tk):
    def __init__(self,canvas):
        # not initialize a second Tk instance ==> tk.Tk.__init__(self)
        self.x = self.y = 0
        #  attach the main canvas
        self.canvas = canvas
        self.canvas.pack(side="top", fill = "both", expand = True)

而不是:

class Rectangle(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.x = self.y = 0
        self.canvas.pack(side="top", fill = "both", expand = True)

问题 2 - 没有创建 class Rectangle 的实例。

To use the class Rectangle, an instance shall be created and attached to the canvas instance !!

Before, move the class declaration before Tk() and Canvas() creation.

class Rectangle(tk.Tk):
    def __init__(self):
...
    def release(self, event):
        pass

# Other Classes for arc and pencil begin here

root = Tk()
canvas = Canvas(root, width = w, height = h, bg='#D2B48C', cursor = "cross")
canvas.pack()

# create the Rectangle instance
hRect = Rectangle(canvas)

root.mainloop()

而不是:

root = Tk()
canvas = Canvas(root, width = w, height = h, bg='#D2B48C', cursor = "cross")
canvas.pack()

class Rectangle(tk.Tk):
    def __init__(self):
...
    def release(self, event):
        pass

# Other Classes for arc and pencil begin here

root.mainloop()