简体   繁体   English

我如何格式化 Python Tkinter 中的标签和按钮,例如更改文本的颜色、字体和大小以及按钮的背景?

[英]How can I format labels and buttons in Python Tkinter, for example changing the colour, font and size of the text and the background for buttons?

In this code, I create a button and want it to be font Roboto, size 28 and the colour red.在这段代码中,我创建了一个按钮,并希望它的字体为 Roboto,大小为 28,颜色为红色。 How can I do this?我怎样才能做到这一点?

from tkinter import *
root = Tk()
foo = Button(root, text="foo", command=lambda: print("bar"))
foo.pack()
root.mainloop()

Try this:尝试这个:

from tkinter import *
import tkinter.font as font


root = Tk()
root.geometry("300x200")

# set the font
f = font.Font(size=28)

# create button
foo = Button(root, text='foo!', bg='red', fg='white',command=lambda: print("bar"))
# apply font to button label
foo['font'] = f
# add button to window
foo.pack()

root.mainloop()

Output: Output:

在此处输入图像描述

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

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