티스토리 뷰
창부품의 크기 및 여백을 설정하는 방법에 대해서 알아보도록 하자
창부품의 크기 및 여백을 설정하는것은 2가지 방법이 있다.
>첫번쨰 창부품속성(configure)
>두번째 pack() 옵션
우선 예제를 본후 설명을 이어가도록 하겠다.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # -*- coding : cp949 -*- from tkinter import *
class MyApp: def __init__(self, parent): # ----- 조감을 제어하기 위한 상수들 ----- button_width=6 #버튼의 너비(6글자의크기)
button_padx="2m" #버튼안의 글자와 테두리사이의 가로여백 button_pady="1m" #버튼안의 글자와 테두리사이의 세로여백
buttons_frame_padx="3m" #buttons_frame 바깥 가로여백 buttons_frame_pady="2m" #buttons_frame 바깥 세로여백 buttons_frame_ipadx="3m" #buttons_frame 안쪽 가로여백 buttons_frame_ipady="1m" #buttons_frame 안쪽 세로여백 # ----- 상수 끝 -----
self.myParent=parent
# ----- buttons_frame ----- self.buttons_frame=Frame(parent) self.buttons_frame.pack( ipadx=buttons_frame_ipadx, ipady=buttons_frame_ipady, padx=buttons_frame_padx, pady=buttons_frame_pady)
# ----- BUTTON 1 ----- self.button1=Button(self.buttons_frame, command=self.button1Click) self.button1.configure(text="OK", background="green") self.button1.focus_force() #실행시 기본초점 맞추기 self.button1.configure( width=button_width, padx=button_padx, pady=button_pady) self.button1.pack(side=LEFT) self.button1.bind("<Return>", self.button1Click_a)
# ----- BUTTON 2 ----- self.button2=Button(self.buttons_frame, command=self.button2Click) self.button2.configure(text="Cancel", background="red") self.button2.configure( width=button_width, padx=button_padx, pady=button_pady) self.button2.pack(side=RIGHT) self.button2.bind("<Return>", self.button2Click_a)
def button1Click(self): if self.button1["background"]=="green": self.button1.configure(background="yellow") else: self.button1.configure(background="green")
def button2Click(self): self.myParent.destroy()
def button1Click_a(self, event): self.button1Click()
def button2Click_a(self, event): self.buttton2Click()
root=Tk() myapp=MyApp(root) root.mainloop() |
실행
07행
생성한 OK버튼과 Cancel버튼의 너비를 동일한 크기로 지정하기 위하여
button_width라는 변수에 6이라는 값을 지정한것이다.
33행과 44행에서 사용되어 버튼의 너비크깃속성을 지정할때 사용되었다.
여기서 주목해야할점은 6이라는 숫자가 픽셀이나 mm단위가 아닌, 글자수의 단위라는것이다.
따라서 Cancel글자가 6글자 이므로, 버튼창부품의 너비크기를 6글자로 지정해준것이다.
09행,10행
button_padx, button_pady변수는 생성된 버튼의 내부에 있는 텍스트와 버튼의테두리사이의 여백을 지정할 값을 넣어둔 변수이다.
34행, 35행, 44행, 45행에서 사용되었으며,
padx는 텍스트와 버튼의테두리 사이의 가로여백을 지정해주는 속성이고,
pady는 텍스트와 버튼의테두리 사이의 세로여백을 지정해주는 속성이다.
여기서 사용된 단위는 mm이다.
12행,13행,14행,15행
버튼을 담아놓은 buttons_frame틀의 바깥여백과 안쪽여백을 지정할 값을 넣어놓은 변수이다.
23행, 24행, 25행, 26행에서 사용되었으며,
padx는 buttons_frame의 바깥가로여백을 지정하는 속성이고,
pady는 buttons_frame의 바깥세로여백을 지정하는 속성이다.
ipadx는 buttons_frame의 안쪽가로여백을 지정하는 속성이고,
ipady는 buttons_frame의 안쪽세로여백을 지정하는 속성이다.
※ 속성의 이름은 같지만, 사용된 위치가 다른경우!
25행과 34행을 보면, padx라는 같은 속성의 이름에 값을 설정하고 있다.
하지만 25행은 pack()메서드에서 사용되었으며,
34행은 configure()메서드에서 사용되었다는 차이점이 있다.
pack()에서 사용되는 padx,pady는 창부품의 바깥쪽여백을 의미한다.
따라서 버튼이든 프레임이든 해당 창부품의 바깥쪽여백을 의미하고,
configure()메서드에서 사용된 padx, pady는 특정 창부품에만 지정되어있는 속성으로써
프레임창부품에는 해당 속성이 존재하지 않는다.
참조 : http://coreapython.hosting.paran.com/GUI/Thinking%20in%20Tkinter.htm
'Python > tkinter' 카테고리의 다른 글
Python,tkinter pack() [공간 다루기] (0) | 2016.06.16 |
---|---|
Python,tkinter 틀의 신축성[부품이 있을때와 없을때의 차이] (0) | 2016.06.15 |
Python,tkinter 틀(Frame)의 테두리 및 크기 지정 (1) | 2016.06.09 |
Python,tkinter 크기및여백지정에 사용되는 단위(pixel,mm) (0) | 2016.05.31 |
Python,tkinter 입문 (1) | 2016.05.25 |
- Total
- Today
- Yesterday
- Excel
- highlightthickness
- activeforeground
- onetomany
- highlightbackground
- indicatoron
- disabledforeground
- activebackground
- Module
- FetchType
- 파이썬
- fetch join
- Composite Key
- 폼
- Python
- tkinter command & bind [명령어묶기와 사건묶기] Python
- checkbutton
- IdClass
- JPA
- ManyToOne
- apache
- tkinter
- command
- borderwidth
- Private
- 상수
- Java
- 리눅스
- vba
- Linux
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |