How do you create a Button on a tkinter Canvas?

前端 未结 3 1981
旧时难觅i
旧时难觅i 2020-12-03 16:04

I created a Frame and then a Canvas.
What I want to do next is to add a Button on the Canvas.
However, when I packed the Button I cannot see the Canvas!

Here

3条回答
  •  遥遥无期
    2020-12-03 16:23

    I had the exact same problem. There isn't an official way that I know, but here's a way around it:

    from Tkinter import *
    root = Tk()
    def clicked(event):
        print("pressed")
    canvas1 = Canvas(root, relief = FLAT, background = "#D2D2D2")
    canvas1.pack()
    buttonBG = canvas1.create_rectangle(0, 0, 100, 30, fill="grey40", outline="grey60")
    buttonTXT = canvas1.create_text(50, 15, text="click")
    canvas1.tag_bind(buttonBG, "", clicked) ## when the square is clicked runs function "clicked".
    canvas1.tag_bind(buttonTXT, "", clicked) ## same, but for the text.
    root.mainloop()
    

提交回复
热议问题