Написал функцию DrawLine в tkinter, но при её использовании появляется ошибка:
Написал функцию DrawLine в tkinter, но при её использовании появляется ошибка:
TypeError: DrawLine() missing 4 required positional arguments: x1, y1, x2, and y2
С чем она может быть связана?
Код:
from tkinter import *
from tkinter import ttk
import numpy as np
import matplotlib.pyplot as plt
#Create an instance of Tkinter frame
win= Tk()
#Set the geometry of the window
win.geometry("700x250")
img=np.ones((1400,1400,3))
def DrawLine(x1, y1, x2, y2):
dx=abs(x2-x1)
dy=abs(y2-y1)
if x1<x2:
xs=1
else:
xs=-1
if y1<y2:
ys=1
else:
ys=-1
x=x1
y=y1
if dx>dy:
p=2*y-dx
while x!=x2:
x=x+xs
if p>0:
y=y+ys
p=p+2*dy-2*dx
else:
p=p+2*dy
img[y,x]=0
else:
p=2*x-dy
while y!=y2:
y=y+ys
if p>0:
x=x+xs
p=p+2*dx-2*dy
else:
p=p+2*dx
img[y,x]=0
plt.rcParams["figure.figsize"]=(5,5)
plt.rcParams["figure.dpi"]=80
figure, axes = plt.subplots()
cc = plt.Circle(( x , y,), 50 , alpha=0.1)
axes.set_aspect( 1 )
axes.add_artist( cc )
axes.add_patch(cc)
plt.ylim(-400, 1000)
plt.xlim(-400, 1000)
plt.imshow(img)
plt.show()
def main():
a = int(input("x1: "))
b = int(input("y1: "))
c = int(input("x2: "))
d = int(input("y1: "))
x1 = a
y1 = b
x2 = c
x3 = d
DrawLine(a,b,c,d)
if __name__ == "__main__":
main()
#Create a Button to plot the graph
button= ttk.Button(win, text= "Graph", command= DrawLine)
button.pack()
win.mainloop()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
from tkinter import *
from tkinter import ttk
import numpy as np
import matplotlib.pyplot as plt
win= Tk()
win.geometry("700x250")
img = np.ones((1400, 1400, 3))
def drawLine(x1, y1, x2, y2):
dx = abs(x2 - x1)
dy = abs(y2 - y1)
if x1 < x2:
xs = 1
else:
xs =- 1
if y1 < y2:
ys = 1
else:
ys =- 1
x = x1
y = y1
if dx > dy:
p = 2 * y - dx
while x != x2:
x = x + xs
if p > 0:
y = y + ys
p = p + 2*dy - 2*dx
else:
p = p + 2*dy
img[y, x]=0
else:
p = 2*x - dy
while y != y2:
y = y + ys
if p > 0:
x = x + xs
p = p + 2*dx - 2*dy
else:
p = p + 2*dx
img[y, x]=0
plt.rcParams["figure.figsize"] = (5, 5)
plt.rcParams["figure.dpi"] = 80
figure, axes = plt.subplots()
cc = plt.Circle((x, y,), 50, alpha=0.1)
axes.set_aspect(1)
axes.add_artist(cc)
axes.add_patch(cc)
plt.ylim(-400, 1000)
plt.xlim(-400, 1000)
plt.imshow(img)
plt.show()
def main():
a = int(input("x1: "))
b = int(input("y1: "))
c = int(input("x2: "))
d = int(input("y1: "))
x1 = a
y1 = b
x2 = c
x3 = d
drawLine(a, b, c, d)
# if __name__ == "__main__":
# main()
#Create a Button to plot the graph
button= ttk.Button(win, text= "Graph", command=main) # - DrawLine
button.pack()
win.mainloop()
