我需要在Python Tkinter的画布上使用多个按钮。目的是在按钮之间绘制连接线。我的代码:
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()
运行此代码,我总是在画布中得到按钮1 ,但我尝试调整create_window
函数和anchor=
中的位置。 按钮2 根本不会显示在画布中。
两张照片返回:
62x26 + 10 + 10
的1x1 + 0 + 0。
根据输出 1x1 + 0 + 0 ,按钮2 似乎未呈现。我做错了什么?
修改
我的错误:
button2_window = convas.create_window(10, 50, anchor=NW, window=button1)
应该是
button2_window = convas.create_window(10, 50, anchor=NW, window=button2)