목차
다음으로 살펴볼 위젯은 클릭 불가능한 위젯이다.
텍스트의 정보를 표시하도록 설계되었고, command 속성은 가지고 있지 않지만.
bind()메서드를 활용하여 유사한 동작을 시뮬레이션 할 수 있다.
- Label
label_object = tk_object.Label(master_object, option,...)
label은 두가지 사용 가능한 속성이 있지만. 두 속성중 하나만 사용해야 한다는 것을 기억해야한다.
레이블 속성(property) | 속성의 의미 |
text | 줄바꿈 문자는 문자열 안의 \n으로 표기한다 |
textvariable | 위의 text와 동일하지만, 관찰 가능 변수(StringVar)를 사용하고 변경 사항이 화면에 즉시 표시된다. |
label의 경우 사용 가능한 메서드가 없다.
import tkinter as tk
def to_string(x):
return "Current counter\nvalue is :\n"+ str(x)
def plus():
global counter
counter +=1
text.set(to_string(counter))
counter = 0
root = tk.Tk()
root.title("Label")
button = tk.Button(root, text="Plus(+)", command=plus)
button.pack()
text = tk.StringVar()
label = tk.Label(root, textvariable=text, height=4)
text.set(to_string(counter))
label.pack()
root.mainloop()
text라는 변수를 통해 label의 텍스트를 표현하고 있다.
Plus(+) 의 버튼을 누르면 label에 표현되는 숫자 counter가 str형으로 변형된 상태로 표현된다.
- Message
message_object = tk_object.Message(master_object, option,...)
Label 과 매우 유사하지만, 위젯 크기에 맞게 자동으로 텍스트를 조정하여 서식을 지정할 수 있다.
import tkinter as tk
def do_it_again():
text.set(text.get()+"and again..")
root = tk.Tk()
button = tk.Button(root, text="Do it again", command=do_it_again)
button.pack()
text = tk.StringVar()
message = tk.Message(root, textvariable=text, width=400)
text.set("Do it")
message.pack()
root.mainloop()
해당 코드를 실행시켜보면 text의 크기만한 창이 버튼을 누를수록 너비가 넓어지고
message 의 최대 너비가 400px인 것을 확인할 수 있다.
최대 너비에 도달하면 텍스트가 줄내림이 발생하면서 계속 진행되는 것을 볼 수 있다.
- Frame
frame_object = tk_object.Frame(master_object, option, ...)
frame은 comtainer로서 다른 위젯을 저장하도록 설계되었다.
즉, frame을 사용하여 창의 직사각형 부분을 분리하고, 그 안에 포함된 모든 위젯의 마스터 위젯으로 역할을 수행할 수 있다.
자체 좌표계를 가지고 있어, 위젯을 배치할 때 창의 왼쪽 위 모서리가 아닌 frame의 왼쪽 위 모서리를 기준으로 위치를 측정한다.
또 새 위치로 이동하면 frame 안에 있는 모든 위젯도 함께 이동한다.
프레임 속성(property) | 속성의 의미 |
takefocus | 일반적으로 frame은 포커스를 받지 않는다. 하지만 포커스처럼 동작하고 싶다면 속성의 값을 1로 설정할 수 있다. |
import tkinter as tk
root = tk.Tk()
frame_1 = tk.Frame(root, width=200, height=100, bg="pink")
frame_2 = tk.Frame(root, width=200, height=100, bg="yellow")
button1_1 = tk.Button(frame_1, text="버튼 1, 프레임 1")
button1_2 = tk.Button(frame_1, text="버튼 2, 프레임 1")
button2_1 = tk.Button(frame_2, text="버튼 1, 프레임 2")
button2_2 = tk.Button(frame_2, text="버튼 2, 프레임 2")
button1_1.place(x=10, y=10)
button1_2.place(x=10, y=50)
button2_1.grid(row=0, column=0)
button2_2.grid(row=1, column=1)
frame_1.pack()
frame_2.pack()
root.mainloop()
- LabelFrame
label_frame_object = tk_object.LabelFrame(master_object, option, ...)
눈에 보이는 테두리와 제목이 표시되는 특징을 가지고 있고, 제목은 테두리 선의 12개 위치 중 하나에 배치될 수 있다.
레이블 프레임 속성(property) | 속성의 의미 |
takefocus | Frame과 동일. |
text | LabelFrame의 타이틀 |
labelanchor | 타이틀의 위치로 나침반 좌표를 포함하는 문자열로 정의된다. (나침반 좌표는 아래) |
NW | N | NE | ||
WN | EN | |||
W | E | |||
WS | ES | |||
SW | S | SE |
default : NW
import tkinter as tk
root = tk.Tk()
labelframe1 = tk.LabelFrame(root, text="Label Frame 1", width=200, height=100, bg="pink")
labelframe2 = tk.LabelFrame(root, text="Label Frame 2", width=200, height=100, bg="yellow", labelanchor="se")
button1_1 = tk.Button(labelframe1, text="버튼 1, 프레임 1")
button1_2 = tk.Button(labelframe1, text="버튼 2, 프레임 1")
button2_1 = tk.Button(labelframe2, text="버튼 1, 프레임 2")
button2_2 = tk.Button(labelframe2, text="버튼 2, 프레임 2")
button1_1.place(x=10, y=10)
button1_2.place(x=10, y=50)
button2_1.grid(row=0, column=0)
button2_2.grid(row=1, column=1)
labelframe1.pack()
labelframe2.pack()
root.mainloop()
frame의 테두리를 따라, 예쁜 타이틀이 생긴 것을 확인할 수 있다.
'Developer > Python' 카테고리의 다른 글
[python] GUI programming tkInter (메인창 구성 및 messagebox) (6) | 2025.07.10 |
---|---|
[python] GUI programming tkInter (menu) (3) | 2025.07.09 |
[python] GUI programming tkInter (클릭 가능 위젯) (4) | 2025.07.08 |
[python] GUI programming tkInter (위젯 속성과 메서드) (0) | 2025.07.07 |
[python] GUI programming tkInter (이벤트 처리) (4) | 2025.07.05 |