How to make non-square edges in tkinter?

Member

by larissa , in category: Third Party Scripts , 8 days ago

How to make non-square edges in tkinter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 7 days ago

@larissa 

To make non-square edges in tkinter, you can use the canvas widget and create a custom shape with the desired edges. Here's an example code to create a button with rounded edges using the canvas widget:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root, width=200, height=50, bg='lightgray', highlightthickness=0)
canvas.pack()

# Create a rounded rectangle shape
def round_rectangle(x1, y1, x2, y2, radius=25, **kwargs):
    points = [x1+radius, y1,
              x1+radius, y1,
              x2-radius, y1,
              x2-radius, y1,
              x2, y1,
              x2, y1+radius,
              x2, y1+radius,
              x2, y2-radius,
              x2, y2-radius,
              x2, y2,
              x2-radius, y2,
              x2-radius, y2,
              x1+radius, y2,
              x1+radius, y2,
              x1, y2,
              x1, y2-radius,
              x1, y2-radius,
              x1, y1+radius,
              x1, y1+radius,
              x1, y1]
    canvas.create_polygon(points, **kwargs)

# Draw the rounded rectangle button
round_rectangle(10, 10, 190, 40, radius=20, fill='blue', outline='black')
canvas.create_text(100, 25, text="Click me!", fill='white')

root.mainloop()


This code creates a button with rounded edges using a custom shape defined by the round_rectangle function. You can adjust the radius parameter to customize the roundness of the edges. Feel free to modify the code as needed to create different shapes with non-square edges in tkinter.