我开始学习Python来为视频游戏创建第三方应用程序。在处理应用程序和游戏客户端之间的连接之前,我尝试使用tkinter创建地图。
我在画布上使用按钮和线条。每个Button代表一个位置,通过特定行与另一个Button连接。例如,Button1和Button2通过从(Button1的中心)到(Button2的中心)的线连接。
我创建了一个允许我在特定位置轻松创建按钮的功能:
from tkinter import *
root = Tk()
canvas = Canvas(root)
def system(system_name, position_x, position_y):
Button(root, text = system_name).place(x=position_x, y=position_y, width=60, height=20)
# examples :
system("Location1", 10,10)
system("Location2", 50,50)
canvas.grid()
root.mainloop()
我想创建一个类似的函数来在2个按钮之间创建一条线。手动计算每个按钮的中心位置很容易,但对数千个按钮执行操作会非常耗时。
是否可以创建这样的函数:
def line(system1, system2):
system1 = center position of system1 = (a,b)
system2 = center position of system2 = (c,d)
canvas.create_line((a,b),(c,d))
欢迎任何建议。先感谢您。
编辑:试着用更简单的术语重新提问:
我使用以下函数创建按钮:
def system(system_name, position_x, position_y):
Button(root, text=system_name).place(x=position_x, y=position_y)
示例:
a = system("Location1", 10,20)
b = system("Location2", 40,50)
我认为这个功能看起来像是:
def line(a, b): # line between button a and button b
c = (10 + whatever_width/2, 20 + whatever_height/2)
d = (40 + whatever_width/2, 50 + whatever_height/2)
canvas.create_line(c,d)
有什么想法吗?