U E D R , A S I H C RSS

방울뱀스터디/GUI

창(Window) 만드는법

~cpp 
from Tkinter import *
root = Tk()
root.mainloop()

프레임 만드는법

창� 프레임� 만든후� GUI�체들� 넣어준다.(그냥 root� 해� �다.)
프레임� 창� �역� 나누어 준다고 보면�다.

~cpp 
frame = Frame(root)
frame.pack()

GUI�체들� 만들어 준후� pack� 해주어야 보�게 �다.

Packer �작�리

pack()�� 여러가지 옵션� 설정해 줄수있다.

side=LEFT # 정렬방�(RIGHT, TOP, BOTTOM)
after=button1 # 현재 pack하려는 �체를 button1바로뒤� 만들어 준다.
before=button1 # 현재 pack하려는 �체를 button1바로앞� 만들어 준다.
fill=X # ê°�체를 ì°½ì�˜ Xì•í�¬ê¸°ì—� 맞추어서 만들어준다. Yì•으로ë�„ 가능
padx=5, pady=5 # � �체들 사�� 간격� 설정한다.
ipadx=5, ipady=5 # �체내부� 간격� 설정한다.
anchor=NW # �체위치를 �서쪽으로 설정. fill옵션� 사용하지않아야 제대로 보�꺼 같�...
~cpp 
button.pack(side=LEFT, fill=X, padx=5, pady=10)
버튼ì�„ 왼쪽정렬ì—� ì°½ì�˜ Xì•ë§Œí�¼ì�˜ í�¬ê¸°ë¡œ 여백ì�€ 가로5, 세로10으로 만들어줘ë�¼....

~cpp 
button.place(30, 30, width=70, height=25)
위치 � �기를 임�로 지정

��터 입력창

��터를 입력하는 하얀박스..
~cpp 
entry = Entry(frame)
entry.insert(0, '') # 처�부분� 공백 문�열� 추가
entry.pack(pady=5)

Label

문�를 표시해줄수 있는 �체(�역)...

textWrite = Label(frame, text="Hello World!")
textWrite.pack()

푸시 단추

�반�� 버튼
~cpp 
button = Button(frame, text="Push Button", fg="red", command=frame.quit)
button.pack(side=LEFT)

pack할때 정렬 방�� 정해줄수있다. 여백지정� 가능..

체� 단추

작� 하얀박스모양�고 체�하면 v표시가 �다.
~cpp 
var = IntVar()  # 0� 초기값으로 하는 정수 변수 ()�� 숫�를 넣어주면 그값으로 초기화�.

check = Checkbutton(frame, text="Check Button", variable=var, command=cb)
check.pack()

def cb():
    if var.get() == 1:
        w.configure(text='Variable is Set')
    elif:
        w.configure(text='Variable is Reset')

command는 단추를 눌렀�때 수행하는 함수를 지정한다.

�디오 단추

�그란 모양�� 선�하면 까만�(?)� 표시�다.
� 그룹�서 한개만 선�할수 있다.
~cpp 
var = IntVar()  

radio1 = Radiobutton(frame, text="One", variable=var, value=1)
radio2 = Radiobutton(frame, text="Two", variable=var, value=2)
radio1.pack(anchor=w)
radio2.pack(anchor=w)

같� 그룹내� 있는 �디오 단추들� 같� variable� 공유해야한다.
(역으로 varivable� 같으면 같� 그룹��고 �야하는건있지...???)

radio1단추를 선�하면 var변수� 값� 1��고, radio2단추를 선�하면 var변수� 값� 2가�다.

Radiobutton 함수호ìœì—�서 indicatoron=0ì�„ 넣어주면 ë�¼ë””오버튼모양ì�´ 푸시버튼모양으로 ë�œë‹¤.

메뉴


스�롤 바

스�롤바는 대부분 리스트박스, 캔버스(Canvas)등과 함께 사용�다.
~cpp 
scrollbar = Scrollbar(frame)
scrollbar.pack(side=RIGHT, fill=Y)

수�(수�)스�롤바를 위젯� 연결하기 위해서 2가지 �� 해야한다.
1. 위젯(�체)� yscrollcommand 콜백 함수를 스�롤바 set 메�드� 설정한다.
2. 스�롤바� command를 위젯� yview� 설정하다.

리스트 ��

수� 스�롤바를 추가시킨 리스트 ��...
~cpp 
listbox = Listbox(frame, yscrollcommand=scrollbar.set)  # 1번 작업
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)  # 2번 작업
수� 스�롤바사용시�는 yscrollcommand대신 xscrollcommand, yview대신 xview를 사용..


�스트 �디터

다양한 �스트 문서를 표시해줄수 잇다. 다양한 �트나 �미지등� 가능함. � 수정� 할수있다.
※Label� �스트를 표시만 해줄수 있�..
~cpp 
textArea = Text(frame, width=80, height=20)
textArea.pack()

�스트 삽입 방법

~cpp 
textArea.insert(END, "Hello")
textArea.insert(INSERT, "world")
textArea.insert(1.0, "!!!!!")

# 버튼� �스트 �디터� 추가하는 방법
button = Button(textArea, text="Click")
textArea.window_create(INSERT, window=button)
END문 문서� 마지막� 삽입
INSERT는 현재 커서위치� 삽입
숫�는 삽입위치 지정 ��, 열

window_create대신� image_create를 �용하여 단추를 문서 안� 추가시킬수� 있�.

�스트 삭제 방법

~cpp 
textArea.deletet(1.0, END) # �스트 전체 삭제
textArea.deletet(INSERT)  # 현재 문� 삭제
textArea.deletet(button) # 단추 삭제

�스트 �기

~cpp 
contents = text.get(1.0, END)

전체 문서를 가져다가 contents 변수� 저장한다.

~cpp 
textArea.config(state=DISABLED)
�스트를 �기전용으로 만듬.(state=NORMAL로 해주면 수정가능)

커서위치 알아내기

~cpp 
index = textArea.index(INSERT)
index� '1.17'과 같� 값� 리턴�.

표준 대화 ��

대화 ��(Dialog Box)

캔바스(Canvas)

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:35
Processing time 0.0120 sec