0

I would like to display the time inside of a tkinter frame in a stylish way, like this :enter image description here

However I can't find any way to do this, please note the label with the time is centered on my frame. I've also tried putting 2 labels on the same grid cell but the label with hours and minutes is removed when I add the label with the seconds. Is there a way to put 2 labels in one ? or group them ?

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, background = "black")
        self.root = parent

        self.columnconfigure(0, weight = 1, uniform = "x")
        self.columnconfigure(1, weight = 1, uniform = "x")
        self.columnconfigure(2, weight = 1, uniform = "x")
        self.rowconfigure(0, weight = 1, uniform = "x")
        self.rowconfigure(1, weight = 1, uniform = "x")

        self.HM = tk.Label(self, text = getHM(), fg = "white", bg = "black", font = LARGE_FONT)
        self.HM.grid(row = 0, column = 1, pady=10, sticky = 'nwe')

        self.S = tk.Label(self, text = getS(), fg = "white", bg = "black", font = LARGE_FONT)
        self.S.grid(row = 0, column = 1, pady=10, sticky = 'nwe')

        self.update_clock()

    def update_clock(self):
        HM = "10:20:"
        S = "45"
        self.HM.configure(text=HM)
        self.S.configure(text=S)
        self.root.after(1000, self.update_clock)

Thanks in advance ! Shraneid

Shraneid
  • 309
  • 1
  • 2
  • 12
  • 1
    Please show what you've tried. Tkinter has no limitations related to this -- you can have as many labels with as many different fonts as will fit on the screen. – Bryan Oakley Oct 30 '18 at 18:31
  • Done ! I'm not quite sure how to add 2 labels in the same grid cell maybe that's the issue – Shraneid Oct 30 '18 at 18:35
  • 1
    @Shraneid I think you can edit text with tags but adding 2 different labels to the same grid cell can be done with using a frame on that cell then adding the 2 different labels to that frame. – Mike - SMT Oct 30 '18 at 18:38
  • @Shraneid I have updated my example to be a working example showing some updated font sizes. – Mike - SMT Oct 30 '18 at 18:57

1 Answers1

2

Here is a working example that takes some of your code and the use of Font from tkinter to set 2 labels with one smaller size font.

import tkinter as tk
from tkinter.font import Font


class MainPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, background = "black")
        time_frame = tk.Frame(self)
        time_frame.grid(row=0, column=1)
        big_font = Font(family='Helvetica', size=33, weight='bold')
        small_font = Font(family='Helvetica', size=12, weight='bold')
        HM = tk.Label(time_frame, text="{} :".format(getHM()), fg="white", bg="black", font=big_font)
        HM.grid(row=0, column=0, pady=10)

        S = tk.Label(time_frame, text=getS(), fg="white", bg="black", font=small_font)
        S.grid(row=0, column=1, pady=10, sticky='s')

root = tk.Tk()

def getHM():
    return("10 : 00")

def getS():
    return("34")

MainPage(root).grid(row=0, column=0)
root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79