반응형
파이썬 위젯 입력창 Entry

입력창은 사용자의 입력을 받을 수 있는 한 줄로 된 문자열 박스입니다.

입력창(Entry)은 단지 한줄의 입력만을 받을 수 있으며

만약 여러줄의 입력을 원한다면 text위젯을 사용해야 합니다.

파이썬 엔트리 옵션

 

[ 파이썬 위젯 엔트리 예제1 ]

 - 문자열 입력1, 문자열 입력2에 문자열을 입력하고 클릭을 누르면 레이블에 출력되게 해보세요

파이썬 위젯 엔트리
파이썬 위젯 엔트리

[ 소스코드 ]

from tkinter import *

win = Tk()
win.geometry('400x200')
win.title("파이썬 위젯 엔트리 공부하기")

def change():
    txt1 = entry1.get()
    txt2 = entry2.get()
    label3.config(text = txt1 + txt2 )
    
    
label1 = Label(win, text = "문자열 입력1 ")
label1.grid(row=0, column=0)
entry1 = Entry(win, width = 20, bg = "light green")
entry1.grid(row=0, column = 1)

label2 = Label(win, text = "문자열 입력2 ")
label2.grid(row=1, column=0)
entry2 = Entry(win, width = 20, bg = "light blue")
entry2.grid(row=1, column = 1)

button = Button(win, text = "클   릭", command = change)
button.grid(row=2, column=1)

label3 = Label(win, text = "입력받은 문자열이 여기에 출력됩니다")
label3.grid(row=3, column=1)

win.mainloop()

 

[ 파이썬 위젯 엔트리 예제2 ]

- 다음과 같이 두수를 입력받아 +, -, *, / 를 계산하는 프로그램을 만드세요.

파이썬 위젯 엔트리
파이썬 위젯

 

[ 소스 코드 ]

from tkinter import *

win = Tk()
win.geometry('400x200')
win.title("사칙 연산 프로그램")

def click1():
    txt1 = entry1.get()
    txt2 = entry2.get()
    dap = int(txt1) + int(txt2)
    label3.config(text = (txt1) + " + " + (txt2) + " = " + str(dap) + "입니다")     

def click2():
    txt1 = entry1.get()
    txt2 = entry2.get()
    dap = int(txt1) - int(txt2)
    label3.config(text = (txt1) + " - " + (txt2) + " = " + str(dap) + "입니다")     

def click3():
    txt1 = entry1.get()
    txt2 = entry2.get()
    dap = int(txt1) * int(txt2)
    label3.config(text = (txt1) + " x " + (txt2) + " = " + str(dap) + "입니다")     

def click4():
    txt1 = entry1.get()
    txt2 = entry2.get()
    dap = int(txt1) / int(txt2)
    label3.config(text = (txt1) + " / " + (txt2) + " = " + str(dap) + "입니다")

def click5():
    label3.config(text = "두 수의 계산 결과는?")
    entry1.delete(0, END)
    entry2.delete(0, END)

def click6():
    win.destroy()
    
  
label1 = Label(win, text = "수 입력 1 ")
label1.grid(row=0, column=0)
entry1 = Entry(win, width = 10, bg = "light green")
entry1.grid(row=0, column = 1)

label2 = Label(win, text = "수 입력 2 ")
label2.grid(row=1, column=0)
entry2 = Entry(win, width = 10, bg = "light blue")
entry2.grid(row=1, column = 1)

button1 = Button(win, text = " +  ",width = 8,  command = click1)
button1.grid(row=2, column=0)

button2 = Button(win, text = " -  ",width = 8,  command = click2)
button2.grid(row=2, column=1)

button3 = Button(win, text = " X  ", width = 8, command = click3)
button3.grid(row=2, column=2)

button4 = Button(win, text = " /  ",width = 8,  command = click4)
button4.grid(row=2, column=3)


label3 = Label(win, text = "두 수의 계산 결과는?")
label3.grid(row=3, column=1, columnspan=3)


button5 = Button(win, text = " 지우기  ",width = 8,  command = click5)
button5.grid(row=4, column=2)

button5 = Button(win, text = " 종 료  ",width = 8,  command = click6)
button5.grid(row=5, column=2)

win.mainloop()

 

[ 파이썬 위젯 엔트리 예제3 ]

  - 아이디와 비밀번호를 입력하고 로그인 버튼을 누르면 메시지박스에 출력되게 하세요

 - 비밀번호는 '*'로 표시되게 하세요

파이썬 엔트리 위젯
파이썬 엔트리 위젯

 

 

[ 소스 코드 ]

더보기
from tkinter import *
from tkinter import messagebox as msg

win = Tk()
win.geometry("350x200")
win.title("Login")

def click():
    st1 = id2.get()
    st2 = pw2.get()
    msg.showinfo("로그인", "아이디 : " + st1 + "\n비밀번호 : " + st2)
    
id = Label(win, text="아 이 디")
id.place(x=60, y=40)
id2 = Entry(win, width=20)
id2.place(x=130, y=40)

pw = Label(win, text="비밀번호")
pw.place(x=60, y=80)
pw2 = Entry(win, width = 20, show='*')
pw2.place(x=130, y=80)

button = Button(win, text="로그인", command = click)
button.place(x=60, y=130)

win.mainloop()
반응형
Posted by 명문코딩컴퓨터
,