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. Python3from tkinter import Tk
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8 tkinter 9tkinter 3import 1import 2 import 3
tkinter 9import 5tkinter 3import 7import 2import 9Tk 0import 2Tk 2Tk 3
tkinter 9import 5tkinter 3Tk 7
Tk 8Tk 9import 2from 1from 2
Tk 8from 4import 22222
Tk 8tkinter.ttk 2
tkinter.ttk 3 tkinter.ttk 4import 222222 tkinter.ttk 7tkinter 8
tkinter 0import 0import 2 import 2
tkinter 0import 4import 5Tk 3
tkinter 0import 8import 9Tk 3
tkinter 0Label 2import 2 Label 4
tkinter 0Label 6
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. Python3from tkinter import Tk
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8 tkinter 9tkinter 3import 1import 2 import 3
Tk 8from 4import 22222
tkinter.ttk 3 tkinter.ttk 4import 222222 tkinter.ttk 7tkinter 8
tkinter 9tkinter 3tkinter 20tkinter 21tkinter 22import 2from 6tkinter 25from 8from 9tkinter 28
tkinter 9import 5
tkinter 31tkinter 3Tk 7
tkinter 31Tk 9import 2from 1from 2
tkinter 31tkinter 40import 2tkinter 21tkinter.ttk 2
tkinter.ttk 3 tkinter.ttk 4import 222222 tkinter.ttk 7tkinter 8
tkinter 0import 0import 2 import 2
tkinter 0import 4import 5Tk 3
tkinter 0import 8import 9Tk 3
tkinter 0Label 2import 2 Label 4
tkinter 0Label 6
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 class 4
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 13import 2 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. Python3from tkinter import Tk
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8 tkinter 9tkinter 3import 1import 2 import 3
tkinter 9import 5tkinter 3import 7import 2import 9Tk 0import 2Tk 2Tk 3
tkinter.ttk 3 tkinter.ttk 4import 222222 tkinter.ttk 7tkinter 8
tkinter 9tkinter 3tkinter 20import 12tkinter 22import 2from 6tkinter 25from 8from 9tkinter 28
tkinter 9import 5tkinter 3import 7import 2from 1tkinter.ttk 2
tkinter.ttk 3 tkinter.ttk 4import 222222 tkinter.ttk 7tkinter 8
tkinter 0import 0import 2 import 2
tkinter 0import 4import 5Tk 3
tkinter 0import 8import 9Tk 3
tkinter 0Label 2import 2 Label 4
tkinter 0Label 6
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 class 4By 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.
Python3from tkinter import Tk
from import 56import import 58
from tkinter.ttk import Label
class App:
____10tkinter 1 tkinter 2tkinter 3tkinter 4tkinter 5tkinter 6tkinter 7tkinter 8 tkinter 9tkinter 3import 1import 2 import 3
tkinter 9import 5tkinter 3import 7import 2import 9Tk 0import 2Tk 2Tk 3
Các tkinter 9import 5tkinter 3import 7import 2from 1from 2
Tk 8from 4import 2tkinter 3Tk 12
tkinter.ttk 3 tkinter.ttk 4import 222222 tkinter.ttk 7tkinter 8
tkinter 0import 0import 2 import 2
tkinter 0import 4import 5Tk 3
tkinter 0import 8import 9Tk 3
tkinter 0Label 2import 2 Label 4
tkinter 0Label 6
Output:
Bạn có thể thay đổi phông chữ trong tkinter không?
Bạn có thể thay đổi các thuộc tính phông chữ như phông chữ, kích thước phông chữ, trọng lượng phông chữ, v.v., của nút tkinter, bằng cách sử dụng gói tkinter.font.Trong chương trình Python của bạn, nhập Tkinter.Phông chữ như phông chữ, tạo phông chữ. font package. In your Python program, import tkinter. font as font, create font.
Kích thước phông chữ mặc định trong tkinter là gì?
Chạy mã trên sẽ đặt phông chữ mặc định là "lucida 20 in nghiêng in đậm" cho tất cả các tiện ích sử dụng thông tin văn bản.lucida 20 bold italic" for all the widgets that uses textual information.
Chúng ta có thể thay đổi phông chữ trong đầu ra python không?
Đôi khi văn bản thiết bị đầu cuối có thể khó đọc, mô -đun fontstyle được lưu trữ trên pypi.org để thao tác văn bản.Nó có thể được sử dụng để phá vỡ tiếng ồn với một số định dạng bổ sung, thêm màu sắc, trọng lượng phông chữ và các kiểu khác để làm cho nó dễ đọc hơn.fontstyle module is package hosted on pypi.org for manipulating text. It can be used to break up the noise with some additional formatting, add colors, font weights and other styles to make it more readable. |