C-log

👩‍🏫class : 5week 본문

📘Python/👩‍🏫class

👩‍🏫class : 5week

4:Bee 2023. 9. 26. 17:12
728x90

복습을 하자

#make to turtle shape
import turtle
t =turtle.Turtle()
t.shape("turtle")

t.up()          #ready to drawing
#new turtle position
t.goto(-200,-50)#get drawing
t.down()

t.circle(20)    #make a fist Circle
#make a line
t.forward(100)
t.right(-90)    #turn to turtle
t.forward(100)
t.right(90)     #turn to turtle
t.forward(100)
t.right(90)     #turn to turtle
t.forward(100)
t.right(-90)    #turn to turtle
t.forward(100)
#second Circle
t.circle(20)

결과 이미지는 위와 같다.


이번에 우리는 tkinter라는 라이브러리를 이용해서 GUI를 생성해 볼 것이다. 해당 라이브러리를 사용하려면 아래와 같이 코드를 작성하면 된다.

from tkinter import *
root = Tk()
root.title("230x120")
root.title("윈도우 창 만들기")
root.mainloop()

위와 같이 코드를 작성하면 기본 GUI 창을 생성할 수 있다. 해당 결과는 아래와 같다.

 

아직 특정 event를 설정 하지 않았기 때문에 여백이다.

root.geometry()

root.geometry는 해당 GUI 윈도우 창의 크기를 결정하는 함수이다.

root.title()

root.title은 해당 GUI 윈도우 창의 이름을 설정하는 함수이다.

root.mainloop()

root.mainloop()는 사용자 인터페이스의 이벤트를 처리하는 함수이다. 우리가 아직 버튼을 생성하지 않았지만 해당 버튼을 클릭하거나 다른 동작을 수행하면 이벤트가 발생하고 메인 루프는 해당 이벤트를 처리한다.


이번에는 label이라는 event함수를 사용해볼 것이다. 코드는 아래와 같다.

from tkinter import *
root = Tk()
root.title("윈도우 창 만들기")
#geometry는 윈도우 창 크기를 결정 짓는 명령어이다.
root.geometry("230x120")

##label을 결정 짓는 부분이다.
lbl_red = Label(root, text = "red")
lbl_green = Label(root, text = "green")
lbl_white = Label(root, text = "white", font="Times 20 bold italic", fg = "white", bg = "black")

lbl_red.pack()
lbl_green.pack()
lbl_white.pack()
root.mainloop()

 

Label()

해당 함수는 라벨 위젯을 생성하는 함수이다. 

.pack()

생성한 라벨을 윈도우에 배치하기 위해서는 pack()함수를 사용해야 한다.

해당 코드의 결과는 위와 같다.


이제 라벨을 생성해 봤으니 값을 입력하는 input event를 사용해보자. 코드는 아래와 같다.

from tkinter import *
root = Tk()
root.title("pack() 배치 관리자 예제")
root.geometry("230x120")
lbl_white = Label(root, text = "white", font = "Times 16 bold italic", fg = "white", bg = "black")

#input을 처리하는 event이다.
ent_input = Entry(root, width=22)
lbl_white.pack()
ent_input.pack()
root.mainloop()

Entry()

input을 생성하기 위한 코드이다. 


이제  input을 통해서 입력한 값들을 추출하는 함수와 버튼 event를 생성하자.

from tkinter import *
root = Tk()
root.title("버튼과 이벤트")
root.geometry("230x120")

#함수 구현
def proc():
    print("Hello!!")

#command역할은 무엇인가.
btn = Button(root, text = "인사하기", command=proc)
#side는 무엇인가.
btn.pack(side=BOTTOM)
root.mainloop()

Button()

Button()함수는 button을 생성하기 위한 함수이다. 이후에 command라는 매개변수를 사용해서 해당 함수를 발생시킬 수 있다. 버튼을 눌렀을 때 함수 print("Hello")가 콘솔에 찍히는 것을 확인 할 수 있다. 결과는 아래 이미지를 확인하자.

def ~ () :

def()는 함수를 구현 하기 위한 함수이다. 


<실습>

#tkinter 연결하기
from tkinter import *
root = Tk()
root.title("로그인하기")
root.geometry("200x100")

#콘솔에 '로그인입니다.'함수구현
def Login():
    print("로그인입니다")


#아이디 Lable
lbl_ID = Label(root, text = "아이디(ID)")
#비밀번호 Lable
lbl_PW = Label(root, text = "비밀번호(PW)")
#아이디 input
ent_ID = Entry(root, width=27)
#비밀번호 input
ent_PW = Entry(root, width=27)
#login 버튼 
btn_login = Button(root, text = "로그인하기", fg = "white", bg = "black", command=Login)

##pack함수 구역
lbl_ID.pack()
ent_ID.pack()
lbl_PW.pack()
ent_PW.pack()
btn_login.pack(side=BOTTOM)

root.mainloop()

728x90

'📘Python > 👩‍🏫class' 카테고리의 다른 글

👩‍🏫class : 8week  (0) 2023.10.24
👩‍🏫class : 7week  (0) 2023.10.16
👩‍🏫class : 4week  (0) 2023.09.19
👩‍🏫class : 3week  (0) 2023.09.12
👩‍🏫class : 2week  (0) 2023.09.05
Comments