简体   繁体   English

如何在 window 中打开一个形状?

[英]How can i open a shape in a window?

So, i was looking for a way to open a window (to make a game) and i figured out, it needs some shaped decoration, so i was looking in other posts of stack overflow and there were none saying how to make it.所以,我一直在寻找一种方法来打开 window(制作游戏),我想通了,它需要一些形状的装饰,所以我正在寻找堆栈溢出的其他帖子,但没有人说如何制作它。 anyways i need to make it fit in the selected code i found on some of the posts of stack overflow.无论如何,我需要使它适合我在一些堆栈溢出帖子中找到的选定代码。

from tkinter import *

class Main:
    def __init__ (self,root):
        self.root = root
        self.root.title("Reminder")
        self.root.geometry("222x222")


if __name__ == '__main__':
    root = Tk()
    obj = Main(root)
    root.mainloop()

Well, the window did work, but it was kind of tough searching the way how to make a window, since most of the tutorial videos were really bad, so are those underrated web sites claiming they have the solution of that or something, anyways they are weirdos, anyways, moving to the shapes expectation i tried watching some tutorial videos but like last time, they were really bad, so i gived up until i found this website, so can you guys help me out with that.好吧,window 确实有效,但是搜索如何制作 window 的方式有点困难,因为大多数教程视频都非常糟糕,那些被低估的 web 网站声称他们有解决方案或其他东西,无论如何他们是怪人,无论如何,转向形状期望我试着看一些教程视频,但就像上次一样,它们真的很糟糕,所以我放弃了,直到我找到这个网站,所以你们能帮我解决这个问题吗?

Tkinter has a widget named Canvas that is designed to let you draw shapes. Tkinter 有一个名为Canvas的小部件,旨在让您绘制形状。 Here's a simple example:这是一个简单的例子:

import tkinter as tk

class Main:
    def __init__ (self,root):
        self.root = root

        self.canvas = tk.Canvas(root, width=400, height=400, background="white")
        self.canvas.pack(fill="both", expand=True)

        self.canvas.create_rectangle(10, 10, 110, 110, outline="black", fill="red")
        self.canvas.create_line(10, 150, 210, 150, fill="green", width=4)
        self.canvas.create_oval(10, 210, 210, 310, outline="blue", fill="yellow")

if __name__ == '__main__':
    root = tk.Tk()
    obj = Main(root)
    root.mainloop()

在此处输入图像描述

If you're trying to make a game, I highly suggest pygame over tkinter because tkinter is not really suited for stuff like this (although it is possible, quality won't be as good pygame ).如果你想制作游戏,我强烈建议pygame而不是tkinter ,因为tkinter不太适合这样的东西(尽管有可能,质量不会像pygame那样好)。

Nevertheless, if you do still intend to use tkinter , you might want to know the limitations first.尽管如此,如果您仍然打算使用tkinter ,您可能想先了解这些限制。 All shapes will have to "labels" with images in it, so essentially it will be all rectangles.所有形状都必须在其中“标记”图像,因此基本上都是矩形。

Click here for a post on how to do that单击此处获取有关如何执行此操作的帖子

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

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