如何使用python更改tkinter中按钮和框架的字体和大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20588417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change font and size of buttons and frame in tkinter using python?
提问by Abkb
This is the code that i used to generate a simple text box and a button in tkinter.
这是我用来在 tkinter 中生成一个简单的文本框和一个按钮的代码。
What should be the parameters to have a better look of the frame and buttons?
应该是什么参数才能更好地查看框架和按钮?
root = Tk.Tk()
def submit():
query = entry.get()
retrieve(query)
entry = Tk.Entry(root)
entry.pack()
button = Tk.Button(root, text='submit', command=submit)
button.pack()
root.mainloop()
采纳答案by Mark Mikofski
UPDATE: The New Mexico Tech tkinter website has been archived on GitHub.
更新:新墨西哥科技 tkinter 网站已在 GitHub 上存档。
First the best reference for Tkinter is this New Mexico Tech website. In the toc you will find a section on fonts, and in the section on Button widgetsyou'll find the option font.
首先,Tkinter 的最佳参考是这个 New Mexico Tech 网站。在目录中,您会找到有关 fonts的部分,在有关 Button 小部件的部分中,您会找到选项font。
you must have a Tkinter object to create a font
你必须有一个 Tkinter 对象来创建字体
Python-2
Python-2
Support for Python-2 has officially ended as of Jan 1, 2020
自 2020 年 1 月 1 日起,对 Python-2 的支持已正式结束
from Tkinter import * # Note: UPPER case "T" in Tkinter
import tkFont
root = Tk()
Python-3
Python-3
Python-3 Tk wrappers differ from Python-2
from tkinter import * # Note: lower case "t" in tkinter
from tkinter import font as tkFont # for convenience
root = Tk()
create a font like the example from New Mexico Tech website
创建类似于 New Mexico Tech 网站示例的字体
helv36 = tkFont.Font(family='Helvetica', size=36, weight='bold')
# you don't have to use Helvetica or bold, this is just an example
(Note: recall for Python-3 fontwas imported as tkFontfor convenience)
(注意:为方便起见font,导入了 Python-3 的召回tkFont)
now you can set the font for buttoncreated from Buttonin the original post
现在您可以设置button从Button原始帖子中创建的字体
button['font'] = helv36
The size of the button will depend on your geometry manager, EG: gridor pack. Only the gridmethod is covered in the layouts sectionby New Mexico Tech site, but effbot.orgis also a great reference and he covers packpretty well.
按钮的大小取决于您的几何管理器 EG:grid或pack。New Mexico Tech 站点grid的布局部分仅介绍了该方法,但effbot.org也是一个很好的参考,他的介绍pack非常好。
try: # Python-2
from Tkinter import *
import tkFont
except ImportError: # Python-3
from tkinter import *
from tkinter import font as tkFont
# using grid
# +------+-------------+
# | btn1 | btn2 |
# +------+------+------+
# | btn3 | btn3 | btn4 |
# +-------------+------+
root = Tk()
# tkFont.BOLD == 'bold'
helv36 = tkFont.Font(family='Helvetica', size=36, weight=tkFont.BOLD)
btn1 = Button(text='btn1', font=helv36)
btn2 = Button(text='btn2', font=helv36)
btn3 = Button(text='btn3', font=helv36)
btn4 = Button(text='btn4', font=helv36)
btn5 = Button(text='btn5', font=helv36)
root.rowconfigure((0,1), weight=1) # make buttons stretch when
root.columnconfigure((0,2), weight=1) # when window is resized
btn1.grid(row=0, column=0, columnspan=1, sticky='EWNS')
btn2.grid(row=0, column=1, columnspan=2, sticky='EWNS')
btn3.grid(row=1, column=0, columnspan=1, sticky='EWNS')
btn4.grid(row=1, column=1, columnspan=1, sticky='EWNS')
btn5.grid(row=1, column=2, columnspan=1, sticky='EWNS')
Also try ttk.
也试试ttk。
回答by jfs
tkdocs tutorialrecommends using named fonts and styles if you want to tweak the appearences:
如果您想调整外观,tkdocs 教程建议使用命名字体和样式:
import random
try:
import tkinter as Tk
import tkinter.ttk as ttk
import tkinter.font as font
except ImportError: # Python 2
import Tkinter as Tk
import ttk
import tkFont as font
def change_font_family(query, named_font):
named_font.configure(family=random.choice(font.families()))
root = parent = Tk.Tk()
root.title("Change font demo")
# standard named font (everything that uses it will change)
font.nametofont('TkDefaultFont').configure(size=5) # tiny
# you can use your own font
MyFont = font.Font(weight='bold')
query = Tk.StringVar()
ttk.Entry(parent, textvariable=query, font=MyFont).grid() # set font directly
ttk.Button(parent, text='Change Font Family', style='TButton', # or use style
command=lambda: change_font_family(query, MyFont)).grid()
query.set("The quick brown fox...")
# change font that widgets with 'TButton' style use
root.after(3000, lambda: ttk.Style().configure('TButton', font=MyFont))
# change font size for everything that uses MyFont
root.after(5000, lambda: MyFont.configure(size=48)) # in 5 seconds
root.mainloop()


