๐ฉ๐ซclass : 12week
12์ฃผ์ฐจ์์ if๋ฌธ์ ์ฃผ๋ก ๋ค๋ฃจ์์ง๋ง ์์ฃผ ๊ธฐ๋ณธ์ ์ด๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์์ฉ๋ถํฐ ๊ธ์ ์์ฑํ ๊ฒ์ด๋ค.
img์ entry๋ฅผ ์ด์ฉํ if๋ฌธ
from tkinter import *
root = Tk()
root.title("์๋์ฐ ์ฐฝ ๋ง๋ค๊ธฐ")
#geometry๋ ์๋์ฐ ์ฐฝ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ ์ง๋ ๋ช
๋ น์ด์ด๋ค.
root.geometry("230x120")
def pass_check():
score = int(ent.get())
if score >= 70:
#lbl_status.configure(text = "ํฉ๊ฒฉ")
lbl_status.configure(text = "ํฉ๊ฒฉ")
lbl_img = Label(root, text = "ํฉ๊ฒฉ")#image = photo1
lbl_img.place(x=100, y=30)
else :
lbl_status.configure(text="๋ถํฉ๊ฒฉ")
lbl_img = Label(root, image = photo2)
lbl_img.place(x=100, y=30)
photo1 = PhotoImage(file="smile.png")
photo2 = PhotoImage(file="try.png")
lbl = Label(root, text = "์ ์")
ent = Entry(root, width= 20)
btn = Button(root, text = "ํ์ธ", command = pass_check)
lbl_status = Label(root, text = "ํฉ๊ฒฉ์ฌ๋ถ")
lbl.place(x = 10, y = 10)
ent.place(x = 50, y = 10)
btn.place(x = 50, y = 40)
lbl_status.place(x = 50, y = 80)
#์์น๋ฅผ ๋ฐฐ์นํ๋ ค๋ฉด pack์ผ๋ก ์ง์ ํ๋ฉด ์๋๋ค.
#lbl.pack()
#ent.pack()
#btn.pack()
#lbl_status.pack()
root.mainloop()
#https://076923.github.io/posts/Python-tkinter-12/
์ฃผ์์ผ๋ก๋ ์ค๋ช ์ ํ์ง๋ง place๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ pack()์ ์ด์ฉํ๋ฉด ์๋๋ค. ๋ฌผ๋ก pack๋ด๋ถ์์๋ ์์น๊ฐ์ ์กฐ์ ํ ์๋ ์์ง๋ง place๋งํผ ์ ๋ฐํ๊ฒ ์กฐ์ ํ ์ ์๋ค.
์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐฉ์์์ ์ด๋ฏธ์ง์ ์ฃผ์๋ ์ปดํ์ผ์ด ๋๋ pyํ์ผ ๊ณผ ๋๋ฑํ๊ฒ ์์นํด์ผ ํ๋ค. ๋ฟ๋ง ์๋๋ผ configue๋ฅผ ํตํด์ ๋ณ๊ฒฝ๋ ๊ฐ์ ์ค ์ ์๋๋ฐ ์ฌ๊ธฐ์ img๋ฅผ ์ค์๋ ์๊ณ text๋ก ๋ณ๊ฒฝ ํ ์ ์๋ค.
์ง์ ์ ๋ ฅํ ๊ฐ์ ์ง์ ํ์ ๊ตฌ๋ถ
number = input("์ ์๋ฅผ ์
๋ ฅํ์ธ์.->")
#๋์ ํ์๋ฆฌ๋ง ๊ฐ์ง๊ณ ์์ ๊ตฌ๋ฌธํ๋ ๋ฐฉ๋ฒ
last_char = number[-1]
last_num = int(last_char)
if last_num == 0 \
or last_num == 2 \
or last_num == 4 \
or last_num == 6 \
or last_num == 8 :
print("์ง์ ์
๋๋ค.")
else :
print("ํ์ ์
๋๋ค.")
๋ฌดํํ ์ง์ ์ ๋ ค๋ ๊ฐ์ด ์ง์์ธ์ง ํ์ ์ธ์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์ ๋ง์ง๋ง ๋ท ์๋ฆฌ์ ํ์ง์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒฐ์ ๋๊ฒ ๋ง๋ ์ฝ๋์ด๋ค.
๋ฒํผ์ ๊ตฌํํด์ ์ฌ์ง ์์น๋ฅผ ๋ฐ๊ฟ ์ ์๋ ํ์ด์ง๋ฅผ ์ ์
from tkinter import *
root = Tk()
root.title("image ์กฐ์ํ๊ธฐ")
root.geometry("720x450")
#-------------------- image area --------------
#์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ
brain = PhotoImage(file="smile.png")
#์ด๋ฏธ์ง ์์ ฏ ์์ฑ
lbl_img = Label(root, image = brain)
#img์์น ๋ณ์
current_x = 100
current_y = 100
#img ์์ ฏ ์์น ์ค์
lbl_img.place(x = current_x, y = current_y)
def new_pos() : #x,y
#๋งค๊ฐ๋ณ์๋ก ๊ฐ์ ธ์ฌ ์ ์์ผ๋ ์ง์ ์ ์ผ๋ก get์ ํด์ ๊ฐ์ง๊ณ ์์ผํ๋ค.
current_x = x_ent.get()
current_y = y_ent.get()
#์ด๋ฏธ์ง์ ์์น๊ฐ event ์์ ฏ ์์ญ์ ์นจ๋ฒํ์ง ์๊ธฐ ์ํ if๋ฌธ
if int(current_y) > 100 :
lbl_img.place(x = current_x, y = current_y)
print("check", current_x, current_y)
else :
#์ด๊ณผ ๋ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๋ฌธ์ ํ์ฑ
print("Whatch OUT!!")
#--------------- ์์ ฏ ์์ฑ ---------------
x_lbl = Label(root, text = "input pos(x)")
x_ent = Entry(root, width = 20)
y_lbl = Label(root, text = "input pos(y)")
y_ent = Entry(root, width = 20)
#btn_create
btn = Button(root, text = "๋ณ๊ฒฝ", command = new_pos)#lambda : new_pos(x_ent.get(), y_ent.get())
# ---------------์์ ฏ ์์น ์ง์ ---------------
x_lbl.place(x = 10, y = 20)
x_ent.place(x = 90, y = 20)
y_lbl.place(x = 10, y = 50)
y_ent.place(x = 90, y = 50)
#btn_place
btn.place(x = 10, y = 70)
root.mainloop()
ํจ์๋ฅผ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ Button์ command๋ก evnet์ฒ๋ฆฌ๋ฅผ ํ๋ คํ์ผ๋ ๋ฒํผ ํด๋ฆญ์์ ๋ฐ๋ก event์ฒ๋ฆฌ๊ฐ ๋ฐ์ํ๋ ๋ฌธ์ ๊ฐ ์์ด์ ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์๋ค.์ฒซ ๋ฒ์งธ๋ ์์ ์ ์ด๋ธ ๋ฐฉ์์ผ๋ก ์ง์ Entry๋ ๊ฐ์ get์ผ๋ก ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ด ์๊ณ lambda์์ผ๋ก ํ์ด๋ด๋ ๋ฐฉ๋ฒ์ด ์๋ค. ์๋ ์ฝ๋๋ lambda์์ผ๋ก ์์ฑํ ์ฝ๋์ด๋ค. (ํจ์์ Button๋ถ๋ถ๋ง ์๋ ์ฝ๋๋ ค ๋ณ๊ฒฝํ๋ฉด ๋๋ค.)
...
def new_pos(x,y) : #x,y
current_x = x
current_y = y
#์ด๋ฏธ์ง์ ์์น๊ฐ event ์์ ฏ ์์ญ์ ์นจ๋ฒํ์ง ์๊ธฐ ์ํ if๋ฌธ
if int(x) > 100 :
lbl_img.place(x = current_x, y = current_y)
print("check", current_x, current_y)
else :
#์ด๊ณผ ๋ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๋ฌธ์ ํ์ฑ
print("Whatch OUT!!")
...
btn = Button(root, text = "๋ณ๊ฒฝ", command = lambda : new_pos(x_ent.get(), y_ent.get()))#lambda : new_pos(x_ent.get(), y_ent.get())
ํด๋น ์ฝ๋๋ฅผ ํ ์คํธ ํ๊ธฐ์ํด์ ๊ฐ๋จํ๊ฒ ์ ๋ ฅ ๊ฐ์ ํฉ์ฐํ๋ ์ฝ๋๋ ๋ง๋ค์ด ๋ณด์๋ค. ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
from tkinter import *
root = Tk()
root.title("image ์กฐ์ํ๊ธฐ")
root.geometry("300x250")
def add() :
#์ด๋ ๊ฒ ์ง์ ๋ฐ์์ค๋ ๊ฒ ๋ฐ์ ์๋ค.
a = int(a_ent.get())
b = int(b_ent.get())
result = a+b
lbl.configure(text = f"result : {a} + {b} = {result}")
return a+b
def check():
a_result = a_ent.get()
b_result = b_ent.get()
print(a_result)
print(b_result)
lbl.configure(text = f"result : {a_result}, {b_result}")
#์์ ฏ ์ค์
a_ent = Entry(root, width = 20)
b_ent = Entry(root, width = 20)
btn = Button(root,text="add",command = add)
lbl = Label(root, text = "result")
a_ent.pack()
b_ent.pack()
btn.pack()
lbl.pack()
root.mainloop()