在Toplevel设置IT后我无法获得白板 - Tkinter

时间:2018-01-08 19:47:28

标签: python tkinter

当设置为root时,白板功能正常,现在移动到Toplevel弹出窗口,线条只能从0,0向外发出,从左上角创建漂亮的图形线条。不是预期的目标......嗯。

我怀疑这与我的“全球”陈述有关,但我从未陷入过困境。以下是Toplevel在shell画布中的代码:

# WHITEBOARD POPUP
def whiteboardPanel():
    whitePanel = Toplevel(root)
    whitePanel.transient(root)
    whitePanel.title("WHITEBOARD")
    whitePanel.minsize(550,600)
    whitePanel.maxsize(550,600)
    #whitePanel.focus_set()
    #whitePanel.grab_set()
    whitePanel.geometry("550x600+1800+215")

    lastX, lastY = 0, 0

    colour = "black"

    erase = "white"
    thick = 5

    # =========  FUNCTION DEF ==========

    def set_colour_red(event):
       global colour
        colour = "red"
       thick = 5

    def set_colour_green(event):
       global colour
       colour = "green"
       thick = 5

   def set_colour_black(event):
       global colour
       colour = "black"
       thick = 5

   def set_colour_erase(event):
       global colour
       colour = "white"
       thick = 10

   def store_position(event):
       lastX = event.x
       lastY = event.y

   def on_click(event):
       store_position(event)

   def on_drag(event):
       lastX, lastY = 0, 0

       canvas.create_line(lastX, lastY, event.x, event.y, fill=colour, width=thick)
       store_position(event)

canvas = Canvas(whitePanel, width=550, height=600, bg="white")
canvas.pack()
# fill='both', expand=True, padx=6, pady=6)
canvas.bind("<Button-1>", on_click)
canvas.bind("<B1-Motion>", on_drag)
red_id = canvas.create_rectangle(10, 10, 30, 30, fill="red")
green_id = canvas.create_rectangle(10, 35, 30, 55, fill="green")
black_id = canvas.create_rectangle(10, 60, 30, 80, fill="black")
erase_id = canvas.create_rectangle(10, 85, 30, 105, fill="white")

canvas.tag_bind(red_id, "<Button-1>", set_colour_red)
canvas.tag_bind(green_id, "<Button-1>", set_colour_green)
canvas.tag_bind(black_id, "<Button-1>", set_colour_black)
canvas.tag_bind(erase_id, "<Button-1>", set_colour_erase)


....somewhere down below
....root.mainloop()

请注意,其他Toplevel窗口工作正常,并且在绘图问题之外没有问题。

1 个答案:

答案 0 :(得分:0)

通过删除#lastX,lastY = 0,0作为我能够绘制的全局参数,但on_click(事件)Def不起作用,所以我目前处于红色状态,需要找到另一种更改颜色的方法。