I have two texts above each other in canvas (tkinter), and I want to dynamically resize them with the window

44 Views Asked by At

I have two texts in canvas (tkinter), below each other. When I lower the window's width, the text gets smaller and always fits on the screen. However, when I lower the height, the text doesn't get as small as needed and is outside the window (so it's not visible). How do I keep both texts in my window no matter the window size?

from tkinter import *
from time import strftime


# view = okno s časem
view=Tk()
view.configure(background="black")
view.title("Clock")
# view.config(cursor="none")
screen_width = view.winfo_screenwidth()
screen_height = view.winfo_screenheight()
width = view.winfo_width()
height = view.winfo_height()


# velikosti fontu
font_size = 250
view.minsize(width=screen_width // 3, height=screen_height // 3)

def resize_font(event=None):
    global font_size, string, a
    width = view.winfo_width()
    height = view.winfo_height()
    c = int(height / 2)
    buff = len("a")
    d = int(width / (buff if buff >= 7 else 7))
    new_font_size = min(c, d)
    if new_font_size != font_size:
        font_size = new_font_size
        vse.itemconfig(clock, font=('NovaMono '+ str(font_size)))
        vse.itemconfig(stopky, font=('NovaMono '+ str(int(font_size/1.6))))


# vse = canva
vse = Canvas(view, height=screen_height, width=screen_width, bg="black")
clock = vse.create_text(screen_width // 2, screen_height // 2, fill="orange")
vse.itemconfig(clock, font=('NovaMono', int(font_size)))
stopky = vse.create_text(screen_width // 2, (screen_height // 3) * 2.41, fill="orange")
vse.itemconfig(stopky,font=('NovaMono', int(font_size/1.6)))

vse.place(relx=0.5, rely=0.5, anchor=CENTER)


# clock
def time():
    string = strftime('%H:%M:%S')
    vse.itemconfig(clock, text=str(string))
    vse.after(1000, time)

time()


# stopky
a = -1

def stopkyf():
    global solve, a, vse, stopky
    a = a + 1
    vse.itemconfig(stopky, text=a)
    solve = vse.after(60000, stopkyf)

def stopky_reset():
    global solve, a, vse, stopky
    a = -1
    vse.after_cancel(solve)
    stopkyf()

stopkyf()


# fullscreen
view.attributes("-fullscreen", True)
view.bind('<F11>', lambda e: view.attributes("-fullscreen", not view.attributes("-fullscreen")))


# binding
view.bind("<Control-w>", lambda event: view.destroy())
view.bind("<c>", lambda event: stopky_reset())
vse.bind("<Configure>", resize_font)


# mainloop
view.mainloop()

I also had the code with just labels, which worked for resizing window, but if the window was wide and short, there was a black background for the watches. If you know how to fix this, it'd be also happy :D

0

There are 0 best solutions below