알쏭달쏭 공부한거 쓰기
HW8,HW9 본문
9일차 실습 문제 + Test7, 중간 고사 이후 내용 복습
https://colab.research.google.com/drive/1NrGiqH2bslaVbRPzDg-hohmWBQuf83L9#scrollTo=y9YSwBQFRVRu
11일차 강의 자료 p. 13 GUI 프로그램
- 화면 구성 변경하여 아래 링크에 제시했으니 아래 링크 화면도 구현해보세요.
https://colab.research.google.com/drive/1N0pLXs6ZeM-IztL00HVPHVPDFFs0dqWs?usp=sharing
#BMI계산
from tkinter import*
#BMI 지수
def clicked():
result.config(text=f'당신의 체질량 지수(BMI)는 {float(kg.get())/(float(high.get())**2/10000):.2f}')
window=Tk()
Label(window, text='체중(kg)과 키(cm)를 차례로 입력하세요.').grid(row=0,column=0)
kg=Entry(window)
high=Entry(window)
kg.grid(row=1,column=0,sticky=E+S+N+W)
high.grid(row=2, column=0,sticky=E+S+N+W)
bn1=Button(window, text='BMI 계산',command=clicked)
bn1.grid(row=3,column=0)
result=Label(window, text='입력대기 중')
result.grid(row=4, column=0)
window.mainloop()
#버튼 클릭 횟수
from tkinter import*
def clicked_up():
global c
c+=1
la.config(text=f'버튼 클릭 횟수:{c}')
def clicked_down():
global c
c-=1
if c<0:
c=0
la.config(text=f'버튼 클릭 횟수:{c}')
def clicked_setup():
global c
c=0
la.config(text=f'버튼 클릭 횟수:{c}')
window=Tk()
c=0
la=Label(window, text=f'버튼 클릭 횟수:{c}')
la.pack()
b1=Button(window, text='증가',command=clicked_up)
b1.pack(side=LEFT)
b2=Button(window, text='감소',command=clicked_down)
b2.pack(side=LEFT)
b3=Button(window, text='초기화',command=clicked_setup)
b3.pack(side=LEFT)
window.mainloop()
교수님
from tkinter import*
def clicked_up():
global c
c+=1
la.config(text=f'버튼 클릭 횟수:{c}')
def clicked_down():
global c
c-=1
if c<0:
c=0
la.config(text=f'버튼 클릭 횟수:{c}')
def clicked_setup():
global c
c=0
la.config(text=f'버튼 클릭 횟수:{c}')
window=Tk()
c=0
la=Label(window, text=f'버튼 클릭 횟수:{c}')
la.pack()
b1=Button(window, text='증가',command=clicked_up)
b1.pack(side=LEFT)
b2=Button(window, text='감소',command=clicked_down)
b2.pack(side=LEFT)
b3=Button(window, text='초기화',command=clicked_setup)
b3.pack(side=LEFT)
window.mainloop()
2) 섭씨 화씨 변경
화씨=(fah-32)*5/9
섭씨=cel*9/5+32
from tkinter import*
def fahtocel():
fah=float(e1.get())
e2.delete(0,END)
e2.insert(0,(fah-32)*5/9)
def fahtocel1(event):
fah=float(e1.get())
e2.delete(0,END)
e2.insert(0,(fah-32)*5/9)
def celtofah():
cel=float(e2.get())
e1.delete(0,END)
e1.insert(0,cel*9/5+32)
def celtofah1(event):
cel=float(e2.get())
e1.delete(0,END)
e1.insert(0,cel*9/5+32)
window=Tk()
Label(window, text='화씨').grid(row=0, column=0)
Label(window, text='섭씨').grid(row=1,column=0)
e1=Entry(window, width=24)
e2=Entry(window, width=24)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e1.bind('<Return>',fahtocel1)
e2.bind('<Return>',celtofah1)
but1=Button(window, text='화씨->섭씨')
but2=Button(window, text='섭씨->화씨')
frame=Frame(window)
but1=Button(frame, text='화씨->섭씨',command=fahtocel)
but2=Button(frame, text='섭씨->화씨',command=celtofah)
but1.pack(side=LEFT)
but2.pack(side=LEFT)
frame.grid(row=2,column=0,columnspan=2)
window.mainloop()
#계산기
from tkinter import*
#계산기
def sum_():
a=float(e1.get())
b=float(e2.get())
r.config(text=f'결과={a+b}')
def sub_():
a=float(e1.get())
b=float(e2.get())
r.config(text=f'결과={a-b}')
def mul_():
a=float(e1.get())
b=float(e2.get())
r.config(text=f'결과={a*b}')
def div_():
a=float(e1.get())
b=float(e2.get())
r.config(text=f'결과={a/b}')
window=Tk()
window.geometry("500x500")
Label(window, text='숫자1',width=10).grid(row=0, column=0)
Label(window, text='숫자2',width=10).grid(row=1,column=0)
e1=Entry(window, width=15)
e2=Entry(window, width=15)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
but1=Button(window, text='+',command=sum_)
but2=Button(window, text='-',command=sub_)
but3=Button(window, text='*',command=mul_)
but4=Button(window, text='/',command=div_)
but1.grid(row=1,column=2)
but2.grid(row=1,column=3)
but3.grid(row=1,column=4)
but4.grid(row=1,column=5)
r=Label(window, text='결과')
r.grid(row=2,column=0)
window.mainloop()
# 숫자게임
from tkinter import*
import random as rd
dap=rd.randint(1,100)
def try_():
if dap==int(e.get()):
r.config(text='정답')
elif dap>int(e.get()):
r.config(text='크다!')
else:
r.config(text='작다!')
def reset():
global dap
dap=rd.randint(1,100)
r.config(text = f'숫자가 다시 생성되었습니다. 새로운 숫자를 맞춰보세요.{dap} ')
window=Tk()
window.geometry('700x100')
a=Label(window, text=f'숫자 게임에 오신 것을 환영합니다.!{dap}')
a.pack()
e=Entry(window,width=30)
e.pack(side=LEFT)
b1=Button(window, text='시도', fg='green',command=try_)
b2=Button(window, text='초기화',fg='red',command=reset)
b1.pack(side=LEFT)
b2.pack(side=LEFT)
r=Label(window, text='1부터 100사이의 숫자를 입력하시오')
r.pack(side=LEFT)
window.mainloop()
'파이썬 -23여름학기(ㄱㅎㅈ)' 카테고리의 다른 글
HW10 (0) | 2023.07.11 |
---|---|
7-10 (0) | 2023.07.10 |
hw7 (0) | 2023.07.09 |
HW8 정리 (0) | 2023.07.09 |
2023 여름 파이썬 중간 족보 (0) | 2023.07.09 |