코드:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from tkinter import*
 
#function which actions when the button is pushed
def convert():
    f = float(ent.get())
    tmp = float(5 * (f - 32/ 9)
    #print(tmp)
    lbl_2.configure(text = str(tmp))
    return
 
root = Tk()
root.title("temperature program")
root.geometry("230x120")
 
lbl_1 = Label(root, text = "Fahrenheit")
ent = Entry(root)
lbl_2 = Label(root, text = "Celsius")
btn = Button(root, text = "change to Celsius", command = convert)
 
lbl_1.place(x = 10, y = 20)
ent.place(x = 50, y = 20)
btn.place(x = 50, y = 40)
lbl_2.place(x = 50, y = 80)
 
root.mainloop()
 
cs

 

실행결과:

 

설명:

root = Tk()

위도우를 만들어서 root변수에 저장.

 

root.title("temperature program")

만든 윈도우에 이름을 지정.

 

root.geometry("230x120")

윈도우 창의 크기

 

lbl_1 = Label(root, text = "Fahrenheit")

Lable()함수를 이용해서 라벨 생성

 

ent = Entry(root)

Entry()함수를 이용해서 입력창 생성

 

btn = Button(root, text = "change to Celsius", command = convert)

Button()함수를 이용해서 버튼 생성, 함수는 command로 호출한다.

 

def convert():

    f = float(ent.get())

    tmp = float(5 * (f - 32/ 9)

    #print(tmp)

    lbl_2.configure(text = str(tmp))

    return

함수 생성부분,  lbl_2.configure(text = str(tmp)) configure()함수를 이용해서 두번째 라벨의 내용을 결과값으로 바꾸는 함수다.

 

lbl_1.place(x = 10, y = 20)

place()함수를 이용해서 해당 좌표에 라벨을 붙이는 코드.

 

 

 

+ Recent posts