2017-08-12 95 views
1

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

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

我试图像正常font= 'Times',但是这也没有工作...

也许有些问题的解释(我使用Python 3.6.1-anaconda3)?

There is an image(I tried to change font in label "connection" to family "Times"

+0

你怎么知道它不工作?你有错误吗? –

+0

此代码不会生成错误,但不会更改(仅可更改大小)。 –

+0

基于该截图,它看起来像tkinter没有在您的系统上找到任何字体,并且正在回落到位图字体。在创建字体后将以下代码行添加到程序中时,您会得到什么? '打印(def_font.actual())' –

回答

0

它究竟是如何工作的?下面呢,我(使用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() 

screenshot of tkinter window with buttons with two different fonts

0

我在Raspbian OS类似的问题。

我将'Code-Bold'更改为'codebold',它工作。所以对你来说,也许尝试改变Times - > times。