簡體   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?

在這段代碼中,我創建了一個按鈕,並希望它的字體為 Roboto,大小為 28,顏色為紅色。 我怎樣才能做到這一點?

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

嘗試這個:

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:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM