[英]python: tkinter: how can I get the parameters (font, size, color, effect, etc…) of default font TkDefaultFont
I am new at tkinter, and somewhat new at python.我是 tkinter 的新手,而 python 的新手。 I have an application where I need to know the parameters (font used: Arial, Calibri, etc..., size, color: foreground I believe in tkinter, effect: normal, bold, italic, etc..) of the default font TkDefaultFont used by my widgets.
我有一个应用程序,我需要知道默认字体的参数(使用的字体:Arial,Calibri 等...,大小,颜色:前景我相信 tkinter,效果:正常,粗体,斜体等。)我的小部件使用的 TkDefaultFont。 Today I don't even know what color (foreground in tkinter) to go back to after I change it, since I have no way to "read" the present parameter settings.
今天我什至不知道 go 在我更改后返回什么颜色(tkinter 中的前景),因为我无法“读取”当前的参数设置。 I have an Entry widget and will validate the input.
我有一个 Entry 小部件并将验证输入。 If the input is invalid, I will change the color (foreground) to red.
如果输入无效,我会将颜色(前景)更改为红色。 The code below tells you what I have done, what I know and don't know.
下面的代码告诉你我做了什么,我知道什么,不知道什么。 Any help will be very appreciated!
任何帮助将不胜感激!
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
def JMCheckButton2Command(*args):
if JMCheckButton2Variable.get()==1:
JMEntry.config(foreground = 'red')
else:
JMEntry.config(foreground = 'black')
####################### Tk() #######################
JMWindow = Tk()
s=ttk.Style()
print('\nTheme names are ', s.theme_names())
print('\nLayout of style TButton is:\n', s.layout('TButton'))
print('\nLayout of style TEntry is:\n', s.layout('TEntry'))
print("\nOptions available for element 'Button.label':\n",
s.element_options('Button.label'))
print("\nOptions available for element 'Entry.textarea':\n",
s.element_options('Entry.textarea'))
print("\nFont used in style 'TButton': ", s.lookup('TButton', 'font'))
print("\nFont used in style 'TButton': ", s.lookup('TEntry', 'font'))
####################### ttk.Frame #######################
JMFrame2=ttk.Frame(JMWindow, width='1i', height='2i', borderwidth='5',
relief='raised', padding=(5,5))
JMFrame2.grid()
####################### ttk.Entry #######################
JMEntryVariable = StringVar()
JMEntry = ttk.Entry(JMFrame2, textvariable=JMEntryVariable, width = 25)
JMEntry.insert(0, '100') # insert new text at a given index
####################### ttk.Label #######################
JMLabel2Text2Display = StringVar()
JMLabel2Text2Display.set('Enter number: ')
JMLabel2 = ttk.Label(JMFrame2, textvariable = JMLabel2Text2Display,
justify = 'center')
####################### ttk.Checkbutton #######################
JMCheckButton2Variable = IntVar()
JMCheckButton2=ttk.Checkbutton(JMFrame2, text='Change font color',
command = JMCheckButton2Command, variable = JMCheckButton2Variable)
JMLabel2.grid(column=0, row=0)
JMEntry.grid(column=1, row=0)
JMCheckButton2.grid(column=2, row=0, sticky = 'w', padx=25)
JMWindow.mainloop()
In general, Tk options/properties can be set @construction,一般来说,Tk选项/属性可以设置@construction,
or later with calls to configure()
/ config()
.或稍后调用
configure()
/ config()
。
What can be set with config()
, can be queried with cget()
.可以用
config()
设置什么,可以用cget()
查询。
Can also index widgets with option names: widget['option']
).还可以使用选项名称索引小部件:
widget['option']
)。
See tkinter sources, eg under: /usr/lib/python3.6/tkinter/请参阅 tkinter 源,例如:/usr/lib/python3.6/tkinter/
And Tcl/Tk documentation:和 Tcl/Tk 文档:
https://www.tcl.tk/man/tcl/TkCmd/contents.htm https://www.tcl.tk/man/tcl/TkCmd/contents.htm
For available options, you can search under:有关可用选项,您可以在以下位置搜索:
https://www.tcl.tk/man/tcl/TkCmd/options.htm https://www.tcl.tk/man/tcl/TkCmd/options.htm
and within:并且在:
/usr/lib/python3.6/tkinter/__init__.py
Eg if you wanted to see options supported by class Button:例如,如果您想查看 class 按钮支持的选项:
https://www.tcl.tk/man/tcl/TkCmd/button.htm https://www.tcl.tk/man/tcl/TkCmd/button.htm
and within tkinter/__init__.py
: and within tkinter/__init__.py
:
class Button(Widget):
"""Button widget."""
def __init__(self, master=None, cnf={}, **kw):
"""Construct a button widget with the parent MASTER.
STANDARD OPTIONS
activebackground, activeforeground, anchor,
...
import tkinter as tk
root = tk.Tk()
l = tk.Label(
root, text='asdf', font=('Times', 20)
)
l.pack()
tk.Button(
text='query font',
command=lambda: print(l.cget('font'))
).pack()
tk.Button(
text='change font',
command=lambda: l.configure(font=('Courier', 12))
).pack()
tk.Button(
text='query foreground',
command=lambda: print(l.cget('foreground'))
).pack()
tk.Button(
text='change foreground',
command=lambda: l.configure(foreground='#FF0000')
).pack()
root.mainloop()
Another example (different ways to get/set Tk options):另一个例子(获取/设置 Tk 选项的不同方法):
(See /usr/lib/python3.6/tkinter/font.py (has examples @end)) (见/usr/lib/python3.6/tkinter/font.py(有例子@end))
import tkinter as tk
from tkinter.font import Font # Access Font class directly.
from tkinter import font # To access 'weight' (BOLD etc.).
root = tk.Tk()
# printing f1 (without calling actual()) will print its name
f1 = Font(family='times', size=30, weight=font.NORMAL)
# printing f2 will print the tuple values
f2 = ('courier', 18, 'bold') # Can also use plain tuples.
# This will inlcude name for f1 (created with Font class),
# but not for f2 (f2 is just a tuple).
print('Names of currently available fonts:')
for fName in font.names(): print(fName)
label = tk.Label(root, text='Label Text', font=f1)
label.pack()
tk.Button(
text='Change font (config())',
command=lambda: label.config(font=f1)
).pack()
# Note: cannot use assignment inside lambda,
# so using ordinary function as callback here.
def changeFontWithMapNotation(): label['font'] = f2
tk.Button(
text='Change font (index map)',
command=lambda: changeFontWithMapNotation()
).pack()
tk.Button(
text='Read font (cget())',
command=lambda: print('font:', label.cget('font'))
).pack()
tk.Button(
text='Read font (index map)',
command=lambda: print('font:', label['font'])
).pack()
root.mainloop()
After looking around a bit into ttk source:在查看了一下 ttk 源代码后:
/usr/lib/python3.6/tkinter/ttk.py /usr/lib/python3.6/tkinter/ttk.py
and at the official docs:在官方文档中:
https://www.tcl.tk/man/tcl/TkCmd/ttk_intro.htm https://www.tcl.tk/man/tcl/TkCmd/ttk_intro.htm
https://www.tcl.tk/man/tcl/TkCmd/ttk_style.htm https://www.tcl.tk/man/tcl/TkCmd/ttk_style.htm
here is a ttk example, creating & using a minimal custom style:这是一个 ttk 示例,创建和使用最小的自定义样式:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
styleDb = ttk.Style(root)
csName = 'CustomLabelStyle'
# Need to define layout for custom style, before using it.
# Just copy the corresponding layout from ttk.
csLayout = styleDb.layout('TLabel')
styleDb.layout(csName, csLayout)
# Set ttk options, for custom style, in ttk style database.
styleDb.configure(
csName,
font=('Courier',18),
foreground='#22AA55',
background='#AA5522'
)
# Use custom style for specific widget.
label = ttk.Label(root, text='ttk label', style=csName)
label.pack()
ttk.Button(
root, text='default style',
command=lambda: label.config(style='TLabel')
).pack()
ttk.Button(
root, text='custom style',
command=lambda: label.config(style=csName)
).pack()
defaultLabelFont = styleDb.lookup('TLabel', 'font')
print(defaultLabelFont)
fontVar = tk.StringVar(root, styleDb.lookup(csName, 'font'))
ttk.Entry(root, textvariable=fontVar).pack()
ttk.Button(
root,text='set custom style font',
command=lambda: styleDb.configure(
csName, font=fontVar.get()
)
).pack()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.