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.