๐Ÿ“˜Python/๐Ÿ‘ฉ‍๐Ÿซclass

๐Ÿ‘ฉ‍๐Ÿซclass : 12week

4:Bee 2023. 12. 8. 16:28
728x90

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()
728x90