简体   繁体   English

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

[英]Tkinter have 2 different font sizes on the same text

I would like to display the time inside of a tkinter frame in a stylish way, like this : 我想以一种时尚的方式在tkinter框架中显示时间,如下所示: 在此处输入图片说明

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. 我还尝试将2个标签放在同一网格上,但是当我添加带有秒数的标签时,带有小时和分钟的标签将被删除。 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

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. 这是一个有效的示例,它需要一些代码并使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PIL:我可以有多种大小的相同字体的文本吗? (Python) - PIL: can i have multiple sizes of text of the SAME font? (Python) Tkinter多个按钮-不同的字体大小 - Tkinter multiple Buttons - different font sizes Tkinter:同一按钮内的文本使用不同的字体(颜色,大小等)吗? - Tkinter: Use different font (color, size, etc.) for the text within the same button? 是否可以在同一个字符串上使用两种字体大小(对于 label 或按钮)? - is it possible to have two font sizes on the same string (for a label or button)? matplotlib轴标签中可以有两种不同的字体大小吗? - Can I have two different font sizes in a matplotlib axes label? 如何使用 Python 将文本写入具有不同字体大小的文件? - How to write text into a file with different font-sizes using Python? 为什么具有相同数据的列表具有不同的大小? - Why do lists with the same data have different sizes? 引用相同的 dataframe 时,数据帧的大小不同? - dataframes have different sizes when referencing same dataframe? python图中的不同字体大小 - Different font sizes in python plot PyQt5 - 我可以在 label 中文本的不同部分使用不同的字体大小吗? - PyQt5 - Can I use different font sizes on different parts of the text in a label?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM