简体   繁体   中英

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. 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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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