如何使用python更改tkinter中按钮和框架的字体和大小?

时间:2013-12-14 21:22:22

标签: python tkinter tk

这是我用来在tkinter中生成简单文本框和按钮的代码。

什么参数可以更好地了解框架和按钮?

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

3 个答案:

答案 0 :(得分:10)

首先,Tkinter的最佳参考是this New Mexico Tech website。在toc中,您会找到section on fonts,在section on Button widgets中,您会找到font选项。

你必须有一个Tkinter对象来创建一个字体

from Tkinter import *
import tkFont
root = Tk()

创建一个类似新墨西哥技术网站上的示例字体

helv36 = tkFont.Font(family='Helvetica', size=36, weight='bold')
# you don't have to use Helvetica or bold, this is just an example

现在您可以设置按钮字体

button['font'] = helv36

按钮的大小取决于几何管理器,EG:gridpack。新墨西哥技术网站的layouts section仅涵盖grid方法,但effbot.org也是一个很好的参考,他很好地介绍了pack

from Tkinter import *
import 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')

Tkinter Button fonts

同时尝试ttk

答案 1 :(得分:6)

如果你想调整外观,

tkdocs tutorial建议使用命名字体和样式:

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

答案 2 :(得分:1)

使用命令 font = font.Font(size = 20) 更改按钮中文本的外观

import tkinter.font as fnt
import tkinter as tk
r=tk.Tk()
tk.Button(r,text = "Test", font = fnt.Font(size = 20))
r.mainloop()