๐ฉ๐ซclass : 5week
๋ณต์ต์ ํ์
#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 ์ฐฝ์ ์์ฑํ ์ ์๋ค. ํด๋น ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
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()