-1

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. 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.

  • 2
    "Open a shape in a window" can mean many different things. What do you mean by "a shape"? Are you asking how to draw a rectangle or circle inside the window? Or as you asking how to make non-rectangular root window? Something else? – Bryan Oakley Dec 31 '22 at 18:23
  • Opening a shape that appears in a window. – Oscar Hernandez Dec 31 '22 at 18:59
  • @OscarHernandez Look at the `tkinter.Canvas` class. With that, you can easily make different shapes and with some work you can make the shapes interact. – TheLizzard Dec 31 '22 at 19:48
  • I suggest looking at https://python-course.eu/tkinter/canvas-widgets-in-tkinter.php for examples of drawing shapes on tkinter.canvas. – Carl_M Dec 31 '22 at 19:58
  • A quick note to avoid wildcard imports: https://stackoverflow.com/questions/73698351/is-anyone-know-how-to-connect-tkinter-webcam-to-yolov5/73712541#73712541 – D.L Dec 31 '22 at 22:10

2 Answers2

0

Tkinter has a widget named Canvas that is designed to let you draw shapes. 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()

enter image description here

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

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

Nevertheless, if you do still intend to use tkinter, you might want to know the limitations first. 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