Python 使用 tkinter,按钮之间的线条绘制有问题

Python with tkinter, problems with the line drawing between buttons

我目前正在学习 Python 并开始使用 ktinter。我现在的目标是创建一个拖放功能,它创建按钮并用一条线将它们连接起来。

这是我目前拥有的:

from tkinter import *
class Test:
def __init__(self, master):
    X1 = int(input())
    Y1 = int(input())
    X2 = int(input())
    Y2 = int(input())

    self.blackLine = Canvas.create_line(X1, Y1, X2, Y2)



    convas = Canvas(master)
    convas.pack()
    button1 = Button(text = "button 1")
    button1.configure(width = 0, activebackground = "#D2D2D2", relief = GROOVE)
    button1_window = convas.create_window(X1, Y1, 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(X2, Y2, anchor=NW, window=button2)
    button2.update()
    print (button2.winfo_geometry())

现在你可能会看到我正在测试线条图,方法是在开始时在整数中输入一些值,而不是在按钮 1 中使用它们,2_window 按钮放置在 canvas。现在当我启动程序时,我的问题出现了。

一旦我输入了 4、4、10、10 等 4 个数字,它就会给我一个 AttribuiteError 提示:

"AttributeError: 'int' object has no attribute '_create'"

我不确定,但我猜它以某种方式将数字用作字符串,因此我尝试使用 int(input)) 而不是普通的 input() 函数。

问候

您的行需要使用 canvas 实例,而不是 Canvas class。为此,必须在 Canvas 初始化之后移动它。

convas = Canvas(master)
convas.pack()

self.blackLine = convas.create_line(X1, Y1, X2, Y2)