簡體   English   中英

Tkinter 不會更改字體系列

[英]Tkinter does not change font family

我想更改字體系列,但它不起作用。

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

我正在嘗試類似 normal font= 'Times' ,但這也不起作用......

也許解釋器有問題(我使用 python 3.6.1-anaconda3)?

有一個圖像(我試圖將標簽“連接”中的字體更改為“時代”

它究竟是如何不起作用的? 正如您在屏幕截圖中看到的那樣,以下對我有用(使用 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 窗口的屏幕截圖

我在 Raspbian OS 中遇到了類似的問題。

我將“代碼粗體”更改為“代碼粗體”並且它起作用了。 所以對你來說,也許可以嘗試改變 Times -> times。

我在 Linux 中遇到了同樣的問題。 由於 Linux 中的字體受到更多限制,因此您必須使用font.families()列出系統中所有可用的字體,然后從列表中選擇所需的字體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM