1

I want to make tkinter window corners rounded. I tried to many solutions, found same questions on here too but when I tried other peoples solutions it didn't work for me quite well. Here is my code:

import tkinter as tk
from tkinter import *
import tkinter.ttk as ttk
import datetime as dt
import calendar
from ctypes import windll
from time import strftime
from tkinter.ttk import *
from time import strftime
import time
import tempfile
import tkinter

def move_window(event):
    root.geometry(f'+{event.x_root}+{event.y_root}')

root = tk.Tk()
root.overrideredirect(1)
root.bind("<B1-Motion>", move_window)
root.eval('tk::PlaceWindow . center')
root.title("Simple Clock App")
root.geometry('300x200')
root.configure(background='gray')

def clock():
    hour = time.strftime("%H")
    minute = time.strftime("%M")
    day = time.strftime("%A")


    my_label.config(text=hour + ":" + minute)
    my_label.after(1000, clock)

    my_label2.config(text=day)

def update():

    my_label.config(text="New Text")

my_label = Label(root, text="", font=("Segoe UI Variable", 48), foreground='white', background='gray')
my_label.pack(pady=20)

my_label2 = Label(root, text="", font=("Segoe UI Variable", 14), foreground='white', background='gray')
my_label2.pack(pady=10)


clock()

root.mainloop()
waterstyle
  • 15
  • 1
  • 4
  • 3
    What is the problem with [this](https://stackoverflow.com/a/68860459/11106801) approach? – TheLizzard Aug 21 '21 at 16:44
  • 1
    why exactly do you import from `tkinter` **5 times** and from `time` **3 times**, you need only one import for each library, decide how you will import it and then import **once**? – Matiiss Aug 21 '21 at 20:23

1 Answers1

3

This will help you:

from tkinter import Canvas, Tk
from tkinter.constants import BOTH
from tkinter.ttk import Label
import time

def move_window(event): # Moving the window
    root.geometry(f'+{event.x_root}+{event.y_root}')

def round_rectangle(x1, y1, x2, y2, radius=25, **kwargs): # Creating a rounded rectangle
        
        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]

        return canvas.create_polygon(points, **kwargs, smooth=True, fill="#1fa5fe")

root = Tk()
root.overrideredirect(1)
root.bind("<B1-Motion>", move_window)
root.eval('tk::PlaceWindow . center') # Placing the window in the center of the screen
root.title("Simple Clock App")
root.geometry('300x200')
root.config(background='grey')
root.attributes("-transparentcolor", "grey") # So that it doesn't look like a square

canvas = Canvas(root, bg="grey", highlightthickness=0)
canvas.pack(fill=BOTH, expand=1)

round_rectangle(0, 0, 300, 200, radius=70) # Creating the rounded rectangle/window

def clock():
    hour = time.strftime("%H")
    minute = time.strftime("%M")
    day = time.strftime("%A")


    my_label.config(text=hour + ":" + minute)
    my_label.after(1000, clock)

    my_label2.config(text=day)

def update():

    my_label.config(text="New Text")

my_label = Label(canvas, text="", font=("Segoe UI Variable", 48), foreground='white', background='#1fa5fe')
my_label.pack(pady=20)

my_label2 = Label(canvas, text="", font=("Segoe UI Variable", 14), foreground='white', background='#1fa5fe')
my_label2.pack(pady=10)


clock()

root.mainloop()

If you want to make the window more rounded or squared, you can change the radius of rounded_rectangle in line 46.

To change the background color, change the value of fill= with some other color (located in line 32). Make sure you change the background of the labels too.

IJ_123
  • 457
  • 6
  • 13