Nhãn Tkinter được sử dụng để hiển thị một hoặc nhiều dòng, nó cũng có thể được sử dụng để hiển thị bitmap hoặc hình ảnh. Trong bài viết này, chúng tôi sẽ thay đổi kích thước phông chữ của tiện ích nhãn. Để tạo nhãn sử dụng sau:
Cú pháp: Nhãn = Nhãn (cha mẹ, tùy chọn, Mạnh) label = Label(parent, option, …)
Tham số: Cha mẹ: Đối tượng của tiện ích sẽ hiển thị nhãn này, thường là một đối tượng gốc: Để hiển thị một hoặc nhiều dòng văn bản.Image: Để hiển thị một hình ảnh tĩnh: để hiển thị cả văn bản và hình ảnh. Nó chấp nhận trên cùng, dưới, trái, phải, trung tâm. Ví dụ: nếu bạn viết hợp chất = TopImage sẽ hiển thị lên đầu văn bản.
parent: Object of the widget that will display this label, generally a root object
text: To display one or more lines of text.
image: To display a static image
compound: To display both Text and Image. It accepts TOP, BOTTOM, LEFT, RIGHT, CENTER. For example, if you write compound=TOPimage will displayed to the top of Text.
Chúng ta có thể làm điều này bằng các phương pháp khác nhau:
Phương pháp 1: Bằng cách sử dụng thuộc tính phông chữ nhãn. By using Label’s font property.
Python3
from tkinter import Tk
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8
tkinter 9tkinter 3import1import2 import3
tkinter 9import5tkinter 3import7import2import9Tk0import2Tk2Tk3
tkinter 9import5tkinter 3Tk7
Tk8Tk9import2from1from2
Tk8from4import22222
Tk8tkinter.ttk 2
tkinter.ttk 3 tkinter.ttk 4import222222 tkinter.ttk 7tkinter 8
tkinter 0import0import2 import2
tkinter 0import4import5Tk3
tkinter 0import8import9Tk3
tkinter 0Label2import2 Label4
tkinter 0Label6
Output:
Phương pháp 2: Bằng cách sử dụng lớp kiểu. Trong phương pháp này, chúng tôi sẽ sử dụng kiểu tùy chỉnh của chúng tôi nếu không tất cả các tiện ích nhãn sẽ có cùng một kiểu.By using Style class. In this method, we will use our custom style otherwise all the Label widgets will get the same style.
Python3
from tkinter import Tk
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8
tkinter 9tkinter 3import1import2 import3
Tk8from4import22222
tkinter.ttk 3 tkinter.ttk 4import222222 tkinter.ttk 7tkinter 8
tkinter 9tkinter 3tkinter 20tkinter 21tkinter 22import2from6tkinter 25from8from9tkinter 28
tkinter 9import5
tkinter 31tkinter 3Tk7
tkinter 31Tk9import2from1from2
tkinter 31tkinter 40import2tkinter 21tkinter.ttk 2
tkinter.ttk 3 tkinter.ttk 4import222222 tkinter.ttk 7tkinter 8
tkinter 0import0import2 import2
tkinter 0import4import5Tk3
tkinter 0import8import9Tk3
tkinter 0Label2import2 Label4
tkinter 0Label6
Output:
Phương pháp 2: Bằng cách sử dụng lớp kiểu. Trong phương pháp này, chúng tôi sẽ sử dụng kiểu tùy chỉnh của chúng tôi nếu không tất cả các tiện ích nhãn sẽ có cùng một kiểu.In the above method, TLabel is the name of the default style. So if you want to create your own style name then always use below syntax
from tkinter.ttk import class4
Các
New.TButton # To override Button Widget’s styles
My.TLabel # To override Label Widget’s Styles
Abc.TEntry # To override Entry Widget’s Styles
tkinter 9tkinter 3tkinter 13import2 tkinter 15tkinter 3tkinter 17TLabel instead of My.TLabel then both the label will have font-size of 25. And importantly, if you use the default style name then you don’t need to provide style property.
Lưu ý: Trong phương thức trên, tlabel là tên của kiểu mặc định. Vì vậy, nếu bạn muốn tạo tên kiểu của riêng mình thì hãy luôn sử dụng bên dưới Cú phápChanging font size using the Default Style Name.
Python3
from tkinter import Tk
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8
tkinter 9tkinter 3import1import2 import3
tkinter 9import5tkinter 3import7import2import9Tk0import2Tk2Tk3
tkinter.ttk 3 tkinter.ttk 4import222222 tkinter.ttk 7tkinter 8
tkinter 9tkinter 3tkinter 20import12tkinter 22import2from6tkinter 25from8from9tkinter 28
tkinter 9import5tkinter 3import7import2from1tkinter.ttk 2
tkinter.ttk 3 tkinter.ttk 4import222222 tkinter.ttk 7tkinter 8
tkinter 0import0import2 import2
tkinter 0import4import5Tk3
tkinter 0import8import9Tk3
tkinter 0Label2import2 Label4
tkinter 0Label6
Phương pháp 2: Bằng cách sử dụng lớp kiểu. Trong phương pháp này, chúng tôi sẽ sử dụng kiểu tùy chỉnh của chúng tôi nếu không tất cả các tiện ích nhãn sẽ có cùng một kiểu.style or font property to any of the Label still both of them got the same font-size and same font-family.
Output:
from tkinter.ttk import class4By using the Font class. In this method, we will create a Font object and then use this to change Font style of any widget. Benefit of this
Các
root: Object of toplevel widget.
family: The font family name as a string.
size: The font height as an integer in points.
weight: ‘bold’/BOLD for boldface, ‘normal’/NORMAL for regular weight.
slant: ‘italic’/ITALIC for italic, ‘roman’/ROMAN for unslanted.
underline: 1/True/TRUE for underlined text, 0/False/FALSE for normal.
overstrike: 1/True/TRUE for overstruck text, 0/False/FALSE for normal.
Python3
from tkinter import Tk
from import56import import58
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8
tkinter 9tkinter 3import1import2 import3
tkinter 9import5tkinter 3import7import2import9Tk0import2Tk2Tk3
Các
tkinter 9import5tkinter 3import7import2from1from2
Tk8from4import2tkinter 3Tk12
tkinter.ttk 3 tkinter.ttk 4import222222 tkinter.ttk 7tkinter 8
tkinter 0import0import2 import2
tkinter 0import4import5Tk3
tkinter 0import8import9Tk3
tkinter 0Label2import2 Label4
tkinter 0Label6
Output: