简体   繁体   中英

Python Tkinter - creating multiple polygons in the same canvas

I'm trying to work in 3D, using Tkinter to draw the polygons involved. But early on I have come against a problem, which is that when I try to create several polygons on a canvas, even in different places, only the last one in the code is displayed. Here is the code I was using to test the function - I'm quite new to using Tkinter, so I had to try it out first:

import tkinter as tk

master = tk.Tk()
canvas = tk.Canvas(master, width=200, height=100)
canvas.pack()
def do():
    canvas.create_polygon(-200, -200, -200, 0, 200, 0, 200, -200, fill='red')
    canvas.create_polygon(-200, 200, -200, 0, 200, 0, 200, 200, fill='green')
    master.update()

master.after(2000, do)
tk.mainloop()

When I run this, the result is a small Tk window with a green rectangle. As you can see, it should be a square with a red top half and green bottom half. I have no idea why only the last one appears. I have also tried it with more than two polygons. I use a Chromebook, with the latest version of Miniconda Python, if that makes any difference.

It is happening because of the size of your canvas , it is too small, if you draw on a canvas and some of your coordinates are out of the range of he canvas, the area that is outside of it is not displayed, only what is inside of the canvas range shows. Increase your canvas size and I recommend using a different color for your canvas so that you can see how big it is and draw in its range (change the color back to screen color after you are done drawing)

canvas.config(bg = "blue")

This will change your canvas to red so you can see where it is, and then just change your coordinates to make them all be in the canvas.

Change the width and height of canvas to like 1000 or anything you want so that it can be bigger

You could try something like:

canvas = tk.Canvas(master, width=200, height=200)
canvas.pack()
def do():
    canvas.create_polygon(-200, 200, -200, 0, 200, 0, 200, 200, fill='green')
    canvas.create_polygon(-100, 100, -100, 0, 200, 0, 200, 100, fill='red')

master.update()

The coordinate 0,0 is the upper-left corner of the canvas. Part of your drawing (including all but a one-pixel sliver of the red rectangle) is above and to the left, outside the boundaries of the window.

First the red rectangle is drew outside the viewable area of canvas as (0, 0) is at the upper-left corner of canvas initially.

If you call canvas.config(scrollregion=canvas.bbox('all') at the end of do() and make the canvas large enough to show the two rectangles, then you will see both rectangles:

import tkinter as tk

master = tk.Tk()
canvas = tk.Canvas(master, width=400, height=400)  # make canvas big enough to see the rectangles
canvas.pack()

def do():
    canvas.create_polygon(-200, -200, -200, 0, 200, 0, 200, -200, fill='red')
    canvas.create_polygon(-200, 200, -200, 0, 200, 0, 200, 200, fill='green')
    canvas.create_text(0, 0, text='+') # just show where the origin of canvas is
    canvas.config(scrollregion=canvas.bbox('all'))

master.after(1000, do)
master.mainloop()

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