简体   繁体   English

Python Tkinter - 在同一个 canvas 中创建多个多边形

[英]Python Tkinter - creating multiple polygons in the same canvas

I'm trying to work in 3D, using Tkinter to draw the polygons involved.我正在尝试在 3D 中工作,使用 Tkinter 来绘制所涉及的多边形。 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.但是在早期我遇到了一个问题,那就是当我尝试在 canvas 上创建多个多边形时,即使在不同的地方,也只显示代码中的最后一个。 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:这是我用来测试 function 的代码 - 我对使用 Tkinter 很陌生,所以我必须先尝试一下:

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.当我运行它时,结果是一个带有绿色矩形的小 Tk window。 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.我使用 Chromebook,最新版本的 Miniconda Python,如果这有什么不同的话。

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.这是因为您的canvas的大小,它太小了,如果您在canvas上绘制并且您的某些坐标超出了他 canvas 的范围,那么只有它不在显示的区域之外canvas 范围内显示。 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 size ,我建议为您的 canvas 使用不同的颜色,以便您可以看到它有多大并在其范围内绘图(完成绘图后将颜色更改回屏幕颜色)

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.这会将您的 canvas 更改为红色,这样您就可以看到它的位置,然后只需更改您的坐标以使它们都在 canvas 中。

Change the width and height of canvas to like 1000 or anything you want so that it can be bigger将 canvas 的宽度和高度更改为 1000 或您想要的任何值,以便它可以更大

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.坐标 0,0 是 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.部分绘图(包括除红色矩形的一个像素之外的所有部分)位于 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.首先将红色矩形绘制在 canvas 的可视区域之外,因为(0, 0)最初位于 canvas 的左上角。

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:如果您在do()的末尾调用canvas.config(scrollregion=canvas.bbox('all')并使 canvas 足够大以显示两个矩形,那么您将看到两个矩形:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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