python-3.x - 无法在 tkinter 中更改按钮字体大小

标签 python-3.x user-interface tkinter fonts size

我似乎无法在 tkinter 中更改字体大小!无论我选择哪种尺寸,按钮文本都显示相同。如果我删除整个 stlye行,它显示得更小。

同样,无论我选择什么,字体看起来总是一样的。

我想微调大小和字体,你能帮我吗=?

import tkinter
import tkinter.ttk as ttk
from tkinter import font

root = tkinter.Tk()

frame = ttk.Frame(root)
frame.grid(column=0, row=0)

style = ttk.Style(root)

ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1)

ttk.Style().configure("TButton", font=font.Font(family='wasy10', size=80)) #I can choose any value here instead of "80" and any font like "Helvetica" - nothing will change

root.mainloop()

最佳答案

您不需要导入字体。 ttk 样式有自己的字体参数。
只需将样式放在第一个选项中,将字体大小放在第二个选项中。

我还会使用变量名称来编辑样式。而不是调用:

ttk.Style().configure()

做这个:
style.configure()

看看下面的内容。
import tkinter
import tkinter.ttk as ttk


root = tkinter.Tk()

frame = ttk.Frame(root)
frame.grid(column=0, row=0)

style = ttk.Style(root)
style.configure("TButton", font=('wasy10', 80))

ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)


root.mainloop()

根据 Bryan Oakley 在此处评论中的建议,第二个选项与您尝试使用的内容相近 fort .

此选项保存对字体对象的引用,然后使用它来更新样式。
import tkinter
import tkinter.ttk as ttk
from tkinter import font


root = tkinter.Tk()

frame = ttk.Frame(root)
frame.grid(column=0, row=0)

style = ttk.Style(root)
font = font.Font(family="wasy10", size=80)
style.configure("TButton", font=font)

ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)

root.mainloop()

关于python-3.x - 无法在 tkinter 中更改按钮字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49878388/

相关文章:

python-3.x - 正则表达式识别Python中写成单词的数字?

python - 如何使用 Opencv 缩小图像中的符号

html - 以下设计在技术上称为什么?如何在代码中实现?

python - 如何在 Python 中完成启用/禁用 TkInter 列表框

python - 如何键入具有封闭类类型的提示方法?

python - 从 pandas 系列对象获取值

c - 如何在单个窗口中显示gtk中的文本和按钮?

java - 如何在运行时刷新 JPanel?

python - 获取 tkinter Entry 字段的值以在函数中(本地)使用

python - 为什么打开该文件时Python tkinter崩溃?