Tkinter在同一文本上有2种不同的字体大小

时间:2018-10-30 18:28:13

标签: python tkinter label

我想以时尚的方式在tkinter框架中显示时间,例如:enter image description here

但是我找不到任何方法,请注意带有时间的标签位于我的相框的中心。 我还尝试将2个标签放在同一网格上,但是当我添加带有秒数的标签时,带有小时和分钟的标签将被删除。 有没有办法将两个标签合而为一?或将它们分组?

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)

提前谢谢! Shraneid

1 个答案:

答案 0 :(得分:2)

这是一个有效的示例,它需要一些代码并使用tkinter中的Font来设置2个带有一种较小字体的标签。

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