본문 바로가기

코딩/파이썬 tkinter

[Tkinter] 10장. Label 2단계

tkinter.Label 레벨 2

tkinter.Label에 대한 속성들은 사용자에 의해 변경 가능하며, 대략적으로 다음과 같습니다.

 

  • Label 위젯 내에서 텍스트가 표시되는 위치
  • Label 텍스트 색상
  • Label 텍스트 배경 색상
  • Label 가장자리 경계선 굵기
  • Label 위치에 이미지 삽입
  • Label 텍스트 스타일
  • Label 상태 변경 (활성 vs. 비활성)
  • Label 텍스트에 밑줄 적용

아래의 표는 tkinter.Label에 대하여 설정 가능한 속성을 나타냅니다.

 

STANDARD OPTIONS activebackground,
activeforeground,
anchor,
background,
bitmap,
borderwidth,
cursor,
disabledforeground,
font,
foreground,
highlightbackground,
highlightcolor,
highlightthickness,
image,
justify,
padx,
pady,
relief,
takefocus,
text,
textvariable,
underline,
wraplength
WIDGET-SPECIFIC OPTIONS height,
state,
width

tkinter.Label의 속성 변경하기

[Tkinter] 5장. tkinter.Label에서 소개한 코드를 기반으로 설명합니다.

2020/11/05 - [학습노트/Python - Tkinter] - [Tkinter] 5장. tkinter.Label

 

우선 tkinter.font 모듈을 사용하기 위해 import합니다.

 

import tkinter.font as tkfont

 

그리고 기존의 아래 코드를

 

    # -------------------------------------------------------------------------
    def tkinter_ttk_basics(self):
        tk.Label(self, text='You can\'t connect the dots looking forward.').pack()
        tk.Label(self, text='You can only connect them looking backwards.').pack()
        self.image = tk.PhotoImage(file='data/Steve Jobs.png')
        tk.Label(self, image=self.image).pack()
    # -------------------------------------------------------------------------

 

다음의 코드로 대체합니다.

 

    # -------------------------------------------------------------------------
    def tkinter_ttk_basics(self):
        self.create_label_and_grid('')
        self.create_label_and_grid('foreground', foreground='blue')
        self.create_label_and_grid('background', background='yellow')
        self.create_label_and_grid('underline', underline=0)
        self.create_label_and_grid('width', width=20)  # characters, not pixels
        self.create_label_and_grid('height', height=2)  # characters, not pixels
        self.create_label_and_grid('anchor=tk.W', width=20, anchor=tk.W)
        self.create_label_and_grid('anchor=tk.E', width=20, anchor=tk.E)
        self.create_label_and_grid('borderwidth', borderwidth=5)
        self.create_label_and_grid('state', state=tk.DISABLED)
        self.create_label_and_grid('disabledforeground', state=tk.DISABLED, disabledforeground='red')

        self.image = tk.PhotoImage(file='./data/beer.png')
        self.create_label_and_grid('compound', image=self.image, compound=tk.RIGHT)

        textvar = tk.StringVar()  # tk.StringVar('textvariable')
        textvar.set('textvariable')
        # tk.Label(self, textvariable=textvar, relief=tk.SUNKEN).grid()
        self.create_label_and_grid('', textvariable=textvar)

        font = tkfont.Font(family='', size=22, weight=tkfont.BOLD, slant=tkfont.ITALIC,
                           underline=True, overstrike=True)
        self.create_label_and_grid('font', font=font)

        bitmap = tk.BitmapImage('hourglass')
        self.create_label_and_grid('bitmap', bitmap=bitmap)

    def create_label_and_grid(self, mytext, **kw):
        label = tk.Label(self, text=mytext, relief=tk.SUNKEN, **kw)
        label.grid()
    # -------------------------------------------------------------------------


위의 코드를 추가한 후 실행한 결과는 아래와 같습니다.

 

'코딩 > 파이썬 tkinter' 카테고리의 다른 글

[Tkinter] 12장. Button  (0) 2020.11.11
[Tkinter] 11장. Label (ttk) 2단계  (0) 2020.11.08
[Tkinter] 9장. Grid  (0) 2020.11.08
[Tkinter] 8장. Pack  (0) 2020.11.08
[Tkinter] 7장. Geometry Manager  (0) 2020.11.07