Presentation is loading. Please wait.

Presentation is loading. Please wait.

10장 tkinter로 GUI 만들기.

Similar presentations


Presentation on theme: "10장 tkinter로 GUI 만들기."— Presentation transcript:

1 10장 tkinter로 GUI 만들기

2 이번 장에서 만들 프로그램 (1) 온도 변환 프로그램을 GUI 버전으로 다시 제작해보자
(2) 마우스를 사용하여 화면에 그림을 그리는 프로그램을 작성해보자.

3 tkinter란? tkinter는 파이썬에서 그래픽 사용자 인터페이스(GUI: graphical user interface)를 개발할 때 필요한 모듈

4 tkinter의 유래 tkinter는 예전부터 유닉스 계열에서 많이 사용되던 Tk 위에 객체 지향 계층을 입힌 것이다

5 Tkinter의 위젯들

6

7 단순 위젯과 컨테이너 위젯 단순 위젯: Button, Canavs, Checkbutton, Entry, Label, Message 등이 여기에 속한다. 컨테이너 컴포넌트: 다른 컴포넌트를 안에 포함할 수 있는 컴포넌트로서 Frame, Toplevel, LabelFrame, PanedWindow 등이 여기에 속한다.

8 버튼이 있는 윈도우를 생성해보자 하나의 버튼이 있는 윈도우를 생성해보자

9 엔트리와 레이블 위젯도 사용해보자. 엔트리(Entry) : 사용자로부터 입력 받는 부분 레이블(Label) : 텍스트 표시

10 배치 관리자 압축(pack) 배치 관리자 격자(grid) 배치 관리자 절대(place) 배치 관리자

11 격자 배치 관리자

12 버튼 이벤트 처리 from tkinter import * def process(): print("안녕하세요?")
window = Tk() button = Button(window, text="클릭하세요!", command=process) button.pack() window.mainloop()

13 버튼 이벤트 처리 엔트리 위젝의 0번째 위치에 “100” 추가

14

15 도전문제 앞의 예제에서 “섭씨->화씨“ 버튼을 클릭하면 사용자가 입력한 섭씨온도가 화씨온도로 변환되도록 코드를 추가해보시오 도전문제

16

17 절대 위치 배치 관리자

18 도전문제 화면에 5개의 버튼을 절대 위치를 주어서 배치해보시오 도전문제

19 MyPaint 프로그램 #1 다음과 같이 마우스를 움직여서 화면에 그림을 그리는 윈도우의 그림판과 비슷한 프로그램을 작성해보자.

20 캔버스 위젯 tkinter에서 그림을 그리려면 캔버스(canvas)라는 위젯이 필요하다. Canvas 위젯을 사용하면 많은 그래픽 기능을 사용할 수 있다.

21 MyPaint 프로그램 #2

22 MyPaint 프로그램 #3 앞의 MyPaint 프로그램에서 색상을 변경할 수 있도록 하여보자. 캔버스 위젯 아래에 버튼 “빨강색”을 추가하고 이 버튼을 누르면 색상이 빨강색으로 변경되게 하자.

23

24 도전문제 “녹색”과 “노란색"으로 변경하는 버튼을 추가하시오 도전문제

25 계산기 프로그램 #1 우리는 다음과 같은 계산기를 작성해보자

26 사용자 인터페이스 작성 계산기는 격자 배치 관리자를 사용 하면 될 것이다. 그리고 버튼과 엔트리 위젯만 있으면 된다.

27 계산기 프로그램 #2 from tkinter import * window = Tk()
window.title("My Calculator") display = Entry(window, width=33, bg="yellow") display.grid(row=0, column=0, columnspan=5)

28 계산기 프로그램 #3 button_list = [ '7', '8', '9', '/', 'C',
'4', '5', '6', '*', ' ', '1', '2', '3', '-', ' ', '0', '.', '=', '+', ' ' ] row_index = 1 col_index = 0 for button_text in button_list: Button(window, text=button_text, width=5).grid(row=row_index, column=col_index) col_index += 1 if col_index > 4: row_index += 1

29 계산기 프로그램 #4 def click(key): display.insert(END, key) row_index = 1
col_index = 0 for button_text in button_list: def process(t=button_text): click(t) Button(window, text=button_text, width=5, command=process).grid(row=row_index, column=col_index) col_index += 1 if col_index > 4: row_index += 1 window.mainloop()

30 계산기 프로그램 #5 def click(key): if key == "=":
result = eval(display.get()) s = str(result) display.insert(END, "=" + s) else: display.insert(END, key)


Download ppt "10장 tkinter로 GUI 만들기."

Similar presentations


Ads by Google