在tkinter,python中,我正在尝试输出,所以当单击一个按钮时,一条成角度的线(指向45度方向)被放到画布上。这是我到目前为止尝试过的代码:
from tkinter import *
root = Tk()
root.geometry("900x900")
canvas = Canvas(root, width=800, height=820)
canvas.pack()
png = PhotoImage(file = r'C:\Users\--\Desktop\Programming\GP\europe1.gif') #Map of europe
canvas.create_image(0, 0, image = png, anchor = "nw")
london = canvas.create_rectangle(5, 0, 10, 5, fill='red')
londontext = canvas.create_text(10, 10, text="London") #Creating labelled cities for europe picture
paris = canvas.create_rectangle(5, 0, 10, 5, fill='red')
paristext = canvas.create_text(10, 10, text="Paris")
canvas.move(london, 226, 390)
canvas.move(londontext, 210, 374)
canvas.move(paristext, 230, 447) # Sets correct coords for the two cities
canvas.move(paris, 247, 462)
def gotoparis():
parisline = canvas.create_line(100, 0, 100, 100, 45)
def gotolondon(): # Trying to make a line form between the two cities
londonline = canvas.create_line(100, 0, 100, 100, 45)
def londonclick(event):
londoninterface = Tk()
londoninterface.geometry("400x400")
ll = Label(londoninterface, text="London Airport")
ll.pack()
blank = Label(londoninterface, text="") # Creating new windows
blank.pack()
l1 = Label(londoninterface, text="Send Flight To:")
l1.pack()
toparis = Button(londoninterface, text="Paris", command=gotoparis)
toparis.pack()
def parisclick(event):
parisinterface = Tk()
parisinterface.geometry("400x400")
ll = Label(parisinterface, text="Paris Airport")
ll.pack()
blank = Label(parisinterface, text="")
blank.pack()
l1 = Label(parisinterface, text="Send Flight To:")
l1.pack()
tolondon = Button(parisinterface, text="London")
tolondon.pack()
canvas.tag_bind(london, '<ButtonPress-1>', londonclick) # Makes it so when rectangle is clicked, a new window pops up
canvas.tag_bind(paris, '<ButtonPress-1>', parisclick)
正如你可能会说的那样,我正在努力建造一个类似于机场的机场,你可以将飞机设置到目的地,然后它会创建一条最终结束的线路。这段代码有点难以理解我想要做什么,但简而言之,我想在单击tkinter按钮时在两个画布创建的矩形之间创建一条线。只是为了澄清,我正在使用python 3.5.2 tkinter。 此外,此代码不会创建错误消息或任何类型的错误,但它不会执行我想要它做的事情。这基本上总结了它。
答案 0 :(得分:1)
以下是在画布上创建线条的语法:
Canvas.create_line(x_1, y_1, x_2, y_2, ..., x_n, y_n, **kwargs)
(x_i,y_i)是由线条链接的点,而kwargs设置线条的样式(填充,宽度,...参见here以获得详尽的列表)。所以你不能给出45°角作为参数。
我使用每个方块中心的坐标来绘制它们之间的线。城市的初始中心位于(7.5,2.5),然后由(x_city,y_city)移动:
london = canvas.create_rectangle(5, 0, 10, 5, fill='red')
londontext = canvas.create_text(10, 10, text="London") #Creating labelled cities for europe picture
paris = canvas.create_rectangle(5, 0, 10, 5, fill='red')
paristext = canvas.create_text(10, 10, text="Paris")
# Sets correct coords for the two cities
x_london, y_london = 226, 390
canvas.move(london, x_london, y_london)
canvas.move(londontext, 210, 374)
x_paris, y_paris = 247, 462
canvas.move(paris, x_paris, y_paris)
canvas.move(paristext, 230, 447)
def gotoparis():
parisline = canvas.create_line(x_london + 7.5, y_london + 2.5,
x_paris + 7.5, y_paris + 2.5)
def gotolondon():
londonline = canvas.create_line(x_london + 7.5, y_london + 2.5,
x_paris + 7.5, y_paris + 2.5)
答案 1 :(得分:0)
tkinter画布的line
项目不支持text
项目的“ angle”选项,但是您可以旋转line
(或polygon
)通过:
(1)计算旋转项目的新坐标,以及
(2)使用coords()
方法更新项目的坐标。
如果您知道所需线的起点和终点,则可以将其传递到create_line()
方法中,这样您就可以将想要的线以想要的方式倾斜到前面。
当一条线仅包含一个线段/两个点时,旋转一条线很简单,例如coords =(x1,y1,x2,y2)。但是请注意,您可以添加其他点/坐标来制作更复杂的线。