繁体   English   中英

Tkinter 字体在 python 3.6.8(Ubuntu 18.04LTS)上没有改变

[英]Tkinter font not changing on python 3.6.8 (Ubuntu 18.04LTS)

我无法在 tkinter 中更改标签的字体系列。

我已经编写了代码来解释这个问题。 我还附上了一个链接来显示输出。

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Fonts not Changing!")
win.geometry("400x400")

# Times label
label1 = ttk.Label(win, text = "Brown-Times", font = ("Times",15))
label1.grid(row = 0, column = 0, sticky = 'W')

label12 = ttk.Label(win, text = "Brown-times", font = ("times",15))
label12.grid(row = 0, column = 1, sticky = 'W')

# Arial label
label2 = ttk.Label(win, text = "Brown-Arial", font = ("Arial",15))
label2.grid(row = 1, column = 0, sticky = 'W')

label22 = ttk.Label(win, text = "Brown-arial", font = ("arial",15))
label22.grid(row = 1, column = 1, sticky = 'W')

# Courier label
label3 = ttk.Label(win, text = "Brown-courier", font = ("courier",15))
label3.grid(row = 2, column = 0, sticky = 'W')
# Helvetica label
label4 = ttk.Label(win, text = "Brown-Helvetica", font = ("Helvetica",15))
label4.grid(row = 3, column = 0, sticky = 'W')

win.mainloop()

输出的图片链接

https://drive.google.com/open?id=1mcKSEW_Cmn30pFnlLbuaJdki7OlYvkQ9

编辑

我尝试使用现有的字体系列

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Fonts not Changing!")
win.geometry("400x400")

'''
'fangsong ti', 'fixed', 'clearlyu alternate glyphs', 'courier 10 pitch', 'open look glyph', 'bitstream charter', 'song ti', 'open look cursor', 'newspaper', 'clearlyu ligature', 'mincho', 'clearlyu devangari extra', 'clearlyu pua', 'clearlyu', 'clean', 'nil', 'clearlyu arabic', 'clearlyu devanagari', 'gothic', 'clearlyu arabic extra'
'''

# Times label
label1 = ttk.Label(win, text = "Brown-fangsong ti", font = ("fangsong ti",15))
label1.grid(row = 0, column = 0, sticky = 'W')

label12 = ttk.Label(win, text = "Brown-fixed", font = ("fixed",15))
label12.grid(row = 0, column = 1, sticky = 'W')

# Arial label
label2 = ttk.Label(win, text = "Brown-courier", font = ("courier",15))
label2.grid(row = 1, column = 0, sticky = 'W')

label22 = ttk.Label(win, text = "Brown-clearlyu pua", font = ("clearlyu pua",15))
label22.grid(row = 1, column = 1, sticky = 'W')

# Courier label
label3 = ttk.Label(win, text = "Brown-courier", font = ("courier",15))
label3.grid(row = 2, column = 0, sticky = 'W')
# Helvetica label
label4 = ttk.Label(win, text = "Brown-Helvetica", font = ("Helvetica",15))
label4.grid(row = 3, column = 0, sticky = 'W')

win.mainloop()

但是输出是相似的在此处输入图像描述

编辑 2

我已经尝试过使用 tcl 代码和 python 可用的所有基本字体,请看一下区别。

TCL代号

#!/usr/bin/wish

font create myFont1 -family {fangsong ti} -size 18 -weight bold
pack [label .labelVar1 -font myFont1 -text "Hello World"]

font create myFont2 -family fixed -size 18 -weight bold
pack [label .labelVar2 -font myFont2 -text "Hello World"]

font create myFont3 -family {clearlyu alternate glyphs} -size 18 -weight bold
pack [label .labelVar3 -font myFont3 -text "Hello World"]

font create myFont4 -family {courier 10 pitch} -size 18 -weight bold
pack [label .labelVar4 -font myFont4 -text "Hello World"]

font create myFont5 -family {open look glyph} -size 18 -weight bold
pack [label .labelVar5 -font myFont5 -text "Hello World"]

font create myFont6 -family {bitstream charter} -size 18 -weight bold
pack [label .labelVar6 -font myFont6 -text "Hello World"]

font create myFont7 -family {song ti} -size 18 -weight bold
pack [label .labelVar7 -font myFont7 -text "Hello World"]

font create myFont8 -family {open look cursor} -size 18 -weight bold
pack [label .labelVar8 -font myFont8 -text "Hello World"]

font create myFont9 -family newspaper -size 18 -weight bold
pack [label .labelVar9 -font myFont9 -text "Hello World"]

font create myFont10 -family {clearlyu ligature} -size 18 -weight bold
pack [label .labelVar10 -font myFont10 -text "Hello World"]

font create myFont11 -family mincho -size 18 -weight bold
pack [label .labelVar11 -font myFont11 -text "Hello World"]

font create myFont12 -family {clearlyu devangari extra} -size 18 -weight bold
pack [label .labelVar12 -font myFont12 -text "Hello World"]

font create myFont13 -family {clearlyu pua} -size 18 -weight bold
pack [label .labelVar13 -font myFont13 -text "Hello World"]

font create myFont14 -family clearlyu -size 18 -weight bold
pack [label .labelVar14 -font myFont14 -text "Hello World"]

font create myFont15 -family clean -size 18 -weight bold
pack [label .labelVar15 -font myFont15 -text "Hello World"]

font create myFont16 -family nil -size 18 -weight bold
pack [label .labelVar16 -font myFont16 -text "Hello World"]

font create myFont17 -family {clearlyu arabic} -size 18 -weight bold
pack [label .labelVar17 -font myFont17 -text "Hello World"]

font create myFont18 -family {clearlyu devanagari} -size 18 -weight bold
pack [label .labelVar18 -font myFont18 -text "Hello World"]

font create myFont19 -family gothic -size 18 -weight bold
pack [label .labelVar19 -font myFont19 -text "Hello World"]

font create myFont20 -family {clearlyu arabic extra} -size 18 -weight bold
pack [label .labelVar20 -font myFont20 -text "Hello World"]




puts [font families]

在此处输入图像描述

Python代码

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Fonts not Changing!")
win.geometry("400x400")

fontList = ["{fangsong ti}", "fixed", "{clearlyu alternate glyphs}", "{courier 10 pitch}", "{open look glyph}", "{bitstream charter}", "{song ti}", "{open look cursor}", "newspaper", "{clearlyu ligature}", "mincho", "{clearlyu devangari extra}", "{clearlyu pua}", "clearlyu", "clean", "nil", "{clearlyu arabic}", "{clearlyu devanagari}", "gothic", "{clearlyu arabic extra}"]

label = [ttk.Label(win) for i in range(0,len(fontList))]

for i in range(0,len(fontList)):
    label[i].config(text = "Brown-" + fontList[i], font = (fontList[i],18,"bold"))
    label[i].grid(row = i, column = 0, sticky = 'W')

win.mainloop()

在此处输入图像描述

当使用 python 运行这些相同的字体时,它的行为很奇怪,因为您可以看到大多数使用 TCL 代码看起来不同的字体如何变成与 python 相同的字体(即使某些字体发生了变化)。

我还删除了 tcl/tk 并重新安装(8.6 版),即使这样也没有用。 请任何人帮助...!

根据这篇文章,问题似乎出在 Anaconda3 的 python 上,而且很可能他们也不会解决这个问题。
因为我想不惜一切代价更改字体。 我将 python3.6 安装到我的系统中。 然后安装依赖库来运行上述代码并输入sudo python3 filename.py这次一切正常。

结论

如果您想充分利用 tkinter,请使用系统的 python 而不是 anaconda 的 python。

有同样的问题,我这里的解决方案是以超级用户身份执行文件,所以我认为这个问题与 linux 权限有关。

暂无
暂无

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

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