简体   繁体   中英

multiple buttons on a tkinter canvas

I need multiple buttons on a canvas in Python Tkinter. The purpose is to draw connection lines among the buttons. My code:

from tkinter import *

root=Tk()
convas = Canvas(root)
convas.pack()
button1 = Button(text = "button 1")
button1.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button1_window = convas.create_window(10, 10, anchor=NW, window=button1)
button1.update()
print (button1.winfo_geometry())

button2 = Button(text = "button 2")
button2.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
button2_window = convas.create_window(10, 50, anchor=NW, window=button1)
button2.update()
print (button2.winfo_geometry())
root.mainloop()

running this code, I always just got button 1 in the canvas although I tried to adjust the location in the create_window function, and the anchor= . the button 2 does not show in the canvas at all.

The two prints return:

62x26+10+10
1x1+0+0.

Based on the output 1x1+0+0 , the button 2 seems not being rendered. what I am doing wrong?

EDIT:

My mistake:

button2_window = convas.create_window(10, 50, anchor=NW, window=button1)

should be

button2_window = convas.create_window(10, 50, anchor=NW, window=button2)

Your second button code in line 14 says window=button1 . Change it to:

button2_window = convas.create_window(10, 50, anchor=NW, window=button2)

So that button2 shows up on its window like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM