简体   繁体   English

Tkinter 不会更改字体系列

[英]Tkinter does not change font family

I want to change font family, but it does not work.我想更改字体系列,但它不起作用。

from tkinter import *
import tkinter.font as font

class Window:

    def __init__(self):
        root = Tk()
        def_font=font.Font(family='Times')
        root.title("Serial Conection Program")

        self.mainFrame= Frame(root)

        self.portLabel=Label(self.mainFrame, text="Port: ",font= def_font)
    ....
    ....

I was trying something like normal font= 'Times' , but this also does not work...我正在尝试类似 normal font= 'Times' ,但这也不起作用......

Maybe some problem with interpreter(I use python 3.6.1-anaconda3)?也许解释器有问题(我使用 python 3.6.1-anaconda3)?

There is an image( I tried to change font in label "connection" to family "Times"有一个图像(我试图将标签“连接”中的字体更改为“时代”

How exactly is it not working?它究竟是如何不起作用的? The following does for me (using Python 3.6.2) as you can see in the screenshot:正如您在屏幕截图中看到的那样,以下对我有用(使用 Python 3.6.2):

from tkinter import *
import tkinter.font as font

class Window:
    def __init__(self):
        root = Tk()
        #print(font.families())  # print list of what's available
        root.title("Serial Connection Program")

        self.mainFrame = Frame(root)
        self.mainFrame.pack()

        def_font=font.Font(family='Times')
        self.portLabel = Label(self.mainFrame, text="Port1: ", font=def_font)
        self.portLabel.pack()

        my_font=font.Font(family='Arial')
        self.portLabel = Label(self.mainFrame, text="Port2: ", font=my_font)
        self.portLabel.pack()

        root.mainloop()

win = Window()

带有两种不同字体的按钮的 tkinter 窗口的屏幕截图

I had a similar problem in Raspbian OS.我在 Raspbian OS 中遇到了类似的问题。

I changed 'Code-Bold' to 'codebold' and it worked.我将“代码粗体”更改为“代码粗体”并且它起作用了。 So for you, maybe try changing Times -> times.所以对你来说,也许可以尝试改变 Times -> times。

I had the same issue in Linux.我在 Linux 中遇到了同样的问题。 since the fonts are more limited in Linux, you have to list all the fonts available in your system with font.families() then choose the desired font from the list.由于 Linux 中的字体受到更多限制,因此您必须使用font.families()列出系统中所有可用的字体,然后从列表中选择所需的字体。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM