본문 바로가기
SW 코딩/GUI, tkinter

[파이썬 python] tkinter - 그리드 grid 심화1

2021. 11. 14.
반응형

 

 

참고사이트: 유튜브 나도코딩

code - 그리드 grid 심화1 

15. 그리드 grid 심화 1.txt
0.00MB

code 설명

import tkinter as tk

root = tk.Tk()  # tkinter root창 생성

root.title("tkinter 공부") #창 이름
root.geometry("500x500+200+200") # 창 크기, 가로 x 세로 + 창 출력 위치 좌표

button1 = tk.Button(root, text="버튼1")
button2 = tk.Button(root, text="버튼2")

'''
button1.pack()
button2.pack()
button1.pack(side="left")
button2.pack(side="right")
'''
'''
button1.grid(row=0, column=0)
button2.grid(row=1, column=1)
'''

# 첫번째 그리드 줄
# padx,pady = 글자 중심으로 네모크기 간격 조절
button_1= tk.Button(root,text="1", padx=10, pady=10)
button_2= tk.Button(root,text="2", padx=10, pady=10)
button_3= tk.Button(root,text="3", padx=10, pady=10)
button_4= tk.Button(root,text="4444", padx=10, pady=10)

# N = north, E = east, W = west, S= south
# sticky = 확장시키기
# padx,pady = grid간의 틈사이 간격 조절
button_1.grid(row=0, column=0, sticky="NEWS", padx=3, pady=3)
button_2.grid(row=0, column=1, sticky="NEWS", padx=3, pady=3)
button_3.grid(row=0, column=2, sticky="NEWS", padx=3, pady=3)
button_4.grid(row=0, column=3, sticky="NEWS", padx=3, pady=3)


# 두번째 그리드 줄
button_5= tk.Button(root,text="5555")
button_6= tk.Button(root,text="6")
button_7= tk.Button(root,text="777")
button_8= tk.Button(root,text="8")

button_5.grid(row=1, column=0, sticky="NEWS", padx=3, pady=3)
button_6.grid(row=1, column=1, sticky="NEWS", padx=3, pady=3)
button_7.grid(row=1, column=2, sticky="NEWS", padx=3, pady=3)
button_8.grid(row=1, column=3, sticky="NEWS", padx=3, pady=3)


# 세번째 그리드 줄
button_9= tk.Button(root,text="9")
button_10= tk.Button(root,text="10")
button_11= tk.Button(root,text="11")
button_enter= tk.Button(root,text="Enter")

button_9.grid(row=2, column=0, sticky="NEWS", padx=3, pady=3)
button_10.grid(row=2, column=1, sticky="NEWS", padx=3, pady=3)
button_11.grid(row=2, column=2, sticky="NEWS", padx=3, pady=3)
button_enter.grid(row=2, column=3, rowspan=2, sticky="NEWS", padx=3, pady=3) #현재 위치로부터 아래쪽으로 확장


# 네번째 그리드 줄
button_0= tk.Button(root,text="0")
button_point= tk.Button(root,text=".")

button_0.grid(row=3, column=0, columnspan=2, sticky="NEWS", padx=3, pady=3) #현재 위치로부터 우측으로 확장
button_point.grid(row=3, column=2, sticky="NEWS", padx=3, pady=3)

root.mainloop()

 

결과 화면

버튼이 이쁘게 생성되었지만 "4444" 버튼이 너무 깁니다.

 

버튼 길이를 일정하게 하는건 다음 포스팅에서 해보겠습니다.

댓글


loading