[英]How do I retrieve int data from a list to use in create_oval?
I'm trying to use create_oval to print 5 ovals onto the canvas of a window. 我正在尝试使用create_oval在窗口的画布上打印5个椭圆。 I can't seem to figure out how to retrieve the int data from the circles.dat file to assign them to xpos, ypos, and radius.
我似乎无法弄清楚如何从circle.dat文件中检索int数据,以将它们分配给xpos,ypos和radius。 X and Y are the position of the center of the oval on the canvas.
X和Y是椭圆在画布上的中心位置。 The radius is, well, the radius.
半径就是半径。
Here's my code: 这是我的代码:
from tkinter import *
class Circles:
def __init__(self):
self.window = Tk()
self.window.title("My circles")
self.window.resizable(0,0)
self.viewer = Canvas(self.window, width=640, height=480, bg="white")
try:
open('circles.dat', 'r')
except:
print("File not found")
else:
self.file = open('circles.dat', 'r')
self.lines = self.file.readlines()
self.file.close()
for i in self.lines:
self.lines = i.split()
if self.lines != '/n':
for i in self.lines:
self.xpos = self.lines[1]
self.ypos = self.lines[2]
self.radius = self.lines[3]
self.window.viewer.create_oval(self.xpos, self.ypos, self.radius)
self.viewer.pack(side="top")
self.window.mainloop()
def main():
Circles()
main()
You are not calling create_oval()
correctly. 您没有正确调用
create_oval()
。
First, it should be self.viewer.create_oval()
, note I've removed .window
. 首先,应该是
self.viewer.create_oval()
,注意我已经删除了.window
。 Second, the way you expect the oval to be drawn is not correct. 其次,您期望绘制椭圆的方式不正确。 It is an oval, so it doesn't have a radius as per a circle.
它是一个椭圆形,因此没有圆的半径。 This is how it should be called:
这是应该如何称呼的:
create_oval(x0, x1, y0, y1)
See here for some more help. 请参阅此处以获取更多帮助。
Next, if you want to draw all circles rather than only the last one, you need to indent create_oval()
so that it is inside of the final for loop. 接下来,如果要绘制所有圆而不是仅绘制最后一个圆,则需要缩进
create_oval()
使其位于最终的for循环内。
Finally, make sure that your circle data really is found at index 1, 2 and 3, even though you'll have to make some adjustments. 最后,即使您必须进行一些调整,也请确保确实在索引1、2和3处找到您的圆数据。
from tkinter import *
class Circles:
def __init__(self):
self.window = Tk()
self.window.title("My circles")
self.window.resizable(0,0)
self.viewer = Canvas(self.window, width=640, height=480, bg="white")
try:
open('circles.dat', 'r')
except:
print("File not found")
else:
self.file = open('circles.dat', 'r')
self.lines = self.file.readlines()
self.file.close()
print(self.lines)
for i in self.lines:
self.lines = i.split()
if self.lines != '/n':
for i in self.lines:
self.xpos = self.lines[1]
self.ypos = self.lines[2]
self.radius = self.lines[3]
self.viewer.create_oval(self.xpos, self.ypos, self.radius, self.radius) #Testing, doubled up radius to get output
self.viewer.pack(side="top")
self.window.mainloop()
def main():
Circles()
main()
This work for me: 这项工作对我来说:
from tkinter import *
class Circles:
def __init__(self):
self.window = Tk()
self.window.title("My circles")
self.window.resizable(0,0)
self.viewer = Canvas(self.window, width=640, height=480, bg="white")
try:
open('circles.dat', 'r')
except:
print("File not found")
else:
self.file = open('circles.dat', 'r')
self.lines = self.file.readlines()
self.file.close()
for i in self.lines:
self.lines = i.split()
self.xpos = int(self.lines[0])
self.ypos = int(self.lines[1])
self.radius = int(self.lines[2])
self.viewer.create_oval(self.xpos-self.radius, self.ypos-self.radius, self.xpos+self.radius, self.ypos+self.radius)
self.viewer.pack(side="top")
self.window.mainloop()
def main():
Circles()
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.