简体   繁体   中英

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. 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. X and Y are the position of the center of the oval on the canvas. 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.

First, it should be self.viewer.create_oval() , note I've removed .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.

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM