Python 如何在tkinter中更改画布x、y轴比例?

Python 如何在tkinter中更改画布x、y轴比例?,python,matplotlib,tkinter,tkinter-canvas,Python,Matplotlib,Tkinter,Tkinter Canvas,我想使用tkinter在画布上手动绘制线条,但设置x/y比例,类似于使用plt.ylim(-36503650)和plt.xlim(-53005300)在matplotlib中通常执行的操作 但是,我希望画布窗口本身保持在800x600。我想我可能需要使用matplotlib.transforms,但我不太确定如何使用。以下是我当前的工作代码: import tkinter as tk import matplotlib.pyplot as plt import os def click(e):

我想使用tkinter在画布上手动绘制线条,但设置
x/y
比例,类似于使用
plt.ylim(-36503650)
plt.xlim(-53005300)
在matplotlib中通常执行的操作

但是,我希望画布窗口本身保持在
800x600
。我想我可能需要使用matplotlib.transforms,但我不太确定如何使用。以下是我当前的工作代码:

import tkinter as tk
import matplotlib.pyplot as plt
import os

def click(e):
    # define start point for line
    coords["x"] = e.x
    coords["y"] = e.y

    # create a line on this point and store it in the list
    lines.append(canvas.create_line(coords["x"],coords["y"],coords["x"],coords["y"],fill='white'))

def drag(e):
    # update the coordinates from the event
    coords["x2"] = e.x
    coords["y2"] = e.y

    # Change the coordinates of the last created line to the new coordinates
    canvas.coords(lines[-1], coords["x"],coords["y"],coords["x2"],coords["y2"])

def actionList():
    playerid = player_input.get()
    teamid = team_input.get()
    matchid = match_input.get()
    event = event_input.get()
    phase_type = phase_input.get()
    eventList.append({'player':playerid,'teamid':teamid, 'matchid':matchid, 'event_type':event, 'phase_type':phase_type})
    eventList[len(eventList)-1].update(coords)
    #eventList.append(coords)
    print(eventList)

eventList = []
root = tk.Tk()
width, height = 800, 600

background_image=tk.PhotoImage(file="Pitch2.png")

root.resizable(width=True, height=True)
root.wm_attributes("-topmost", 1)
canvas = tk.Canvas(root, bg="white", width=690, height=550)

# This is the player textbox
player_input = tk.Entry (root)
canvas.create_window(50, 530, window=player_input, width = 100)

playerLabel = tk.Label(root, text='Player Name/Id:')
playerLabel.config(font=('Arial', 10))
canvas.create_window(40, 500, window=playerLabel)

# This is the team textbox
team_input = tk.Entry (root)
canvas.create_window(160, 530, window=team_input, width = 100)

teamLabel = tk.Label(root, text='Team Name/Id:')
playerLabel.config(font=('Arial', 10))
canvas.create_window(155, 500, window=teamLabel)

# This is the match textbox
match_input = tk.Entry (root)
canvas.create_window(270, 530, window=match_input, width = 100)

matchLabel = tk.Label(root, text='Match Name/Id:')
matchLabel.config(font=('Arial', 10))
canvas.create_window(260, 500, window=matchLabel)

# This is the event textbox
event_input = tk.Entry (root)
canvas.create_window(380, 530, window=event_input, width = 100)

eventLabel = tk.Label(root, text='Event Type')
eventLabel.config(font=('Arial', 10))
canvas.create_window(360, 500, window=eventLabel)

# This is the phase type
phase_input = tk.Entry (root)
canvas.create_window(490, 530, window=phase_input, width = 100)

phaseLabel = tk.Label(root, text='Phase Type')
phaseLabel.config(font=('Arial', 10))
canvas.create_window(470, 500, window=phaseLabel)

# This is the button to submit
submit_button = tk.Button(text='Submit', command=actionList)
canvas.create_window(600, 530, window=submit_button)

# End of regular stuff
canvas.pack(fill=tk.BOTH, expand=1) # Stretch canvas to root window size.
image = canvas.create_image(0, 0, anchor=tk.NW, image=background_image)

coords = {"x":0,"y":0,"x2":0,"y2":0}
# keep a reference to all lines by keeping them in a list
lines = []
canvas.bind("<ButtonPress-1>", click)
canvas.bind("<B1-Motion>", drag)

root.mainloop()

将tkinter作为tk导入
将matplotlib.pyplot作为plt导入
导入操作系统
def单击(e):
#定义直线的起点
坐标[“x”]=e.x
坐标[“y”]=e.y
#在此点上创建一条线并将其存储在列表中
lines.append(canvas.create_line(坐标[“x”]、坐标[“y”]、坐标[“x”]、坐标[“y”]、坐标[“y”]、fill='white'))
def阻力(e):
#从事件中更新坐标
坐标[“x2”]=e.x
坐标[“y2”]=e.y
#将上次创建的线的坐标更改为新坐标
canvas.coords(行[-1]、coords[“x”]、coords[“y”]、coords[“x2”]、coords[“y2”])
def actionList():
playerid=player\u input.get()
teamid=team_input.get()
matchid=match_input.get()
event=event\u input.get()
阶段类型=阶段输入。获取()
append({'player':playerid,'teamid':teamid,'matchid':matchid,'event_type':event,'phase_type':phase_type})
事件列表[len(eventList)-1]。更新(coords)
#事件列表。追加(coords)
打印(事件列表)
事件列表=[]
root=tk.tk()
宽度,高度=800600
背景图片=tk.PhotoImage(file=“Pitch2.png”)
根。可调整大小(宽度=真,高度=真)
root.wm_属性(“-top”,1)
canvas=tk.canvas(根,bg=“白色”,宽度=690,高度=550)
#这是播放器文本框
player_input=tk.Entry(根)
canvas.create_窗口(50530,窗口=player_输入,宽度=100)
playerLabel=tk.Label(root,text='Player Name/Id:')
playerLabel.config(字体=('Arial',10))
canvas.create_窗口(40500,窗口=playerLabel)
#这是团队文本框
团队输入=传统条目(根)
canvas.create_窗口(160530,窗口=team_输入,宽度=100)
teamLabel=tk.Label(root,text='Team Name/Id:')
playerLabel.config(字体=('Arial',10))
canvas.create_窗口(155500,窗口=teamLabel)
#这是匹配文本框
匹配输入=tk.Entry(根)
canvas.create_窗口(270530,窗口=匹配_输入,宽度=100)
matchLabel=tk.Label(根,text='Match Name/Id:')
matchLabel.config(字体=('Arial',10))
canvas.create_窗口(260500,窗口=匹配标签)
#这是事件文本框
事件输入=tk.Entry(根)
canvas.create_窗口(380530,窗口=事件_输入,宽度=100)
eventLabel=tk.Label(根,text='Event Type')
eventLabel.config(字体=('Arial',10))
canvas.create_窗口(360500,窗口=事件标签)
#这是相位类型
阶段输入=传统输入(根)
canvas.create_window(490530,window=phase_输入,width=100)
phaseLabel=tk.Label(根,text='Phase Type')
phaseLabel.config(字体=('Arial',10))
canvas.create_window(470500,window=phaseLabel)
#这是要提交的按钮
提交按钮=tk.button(text='submit',command=actionList)
canvas.create_窗口(600530,窗口=提交按钮)
#普通食物的结束
canvas.pack(fill=tk.BOTH,expand=1)#将画布拉伸到根窗口大小。
image=canvas.create_image(0,0,anchor=tk.NW,image=background_image)
coords={x:0,y:0,x2:0,y2:0}
#通过将所有行保留在列表中来保留对它们的引用
行=[]
canvas.bind(“,单击)
canvas.bind(“,拖动)
root.mainloop()

将虚拟坐标转换为画布坐标是一个简单的数学过程。