按下tkinter按钮时修改主窗口

时间:2020-07-23 21:56:26

标签: python tkinter tkinter-canvas

我正在尝试创建一个画布,用户可以在其上绘制矩形,完成绘制后,请按“完成”按钮以移至程序的下一段。

我尝试在create_obstacles函数中创建一个while循环,但这会创建不可单击的按钮框和不可编辑的主画布。

我正在尝试避免这个线性过程,我一直在tkinter代码示例中遇到它。任何帮助将不胜感激!

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.x = self.y = 0
        self.canvas = tk.Canvas(self, width=512, height=512, cursor="cross")
        self.canvas.pack(side="top", fill="both", expand=True)  

    def create_obstacles(self):
        root = tk.Tk()
        root.geometry('100x50')
        btn = Button(root, text ="Click when finished drawing obstacles",command = root.destroy,state="disabled")
        btn.pack()
        root.mainloop()
        while root:

            self.canvas.bind("<ButtonPress-1>", self.on_button_press)
            self.canvas.bind("<B1-Motion>", self.on_move_press)
            self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
            self.rect = None
            self.start_x = None
            self.start_y = None

    def on_button_press(self, event):
        # save mouse drag start position
        self.start_x = event.x
        self.start_y = event.y

        # create rectangle 
        self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill="black")

    def on_move_press(self, event):
        curX, curY = (event.x, event.y)

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

    def on_button_release(self, event):
        pass


if __name__ == "__main__":
    app = ExampleApp()
    app.create_obstacles()
    app.create_starting_point()
    app.mainloop()

0 个答案:

没有答案