Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
27 | 28 | 29 | 30 |
Tags
- 하늘숲펜션
- 산청 황토 펜션
- 지리산 둘레길
- 텐서플로 설치
- HTML
- 하늘숲황토펜션
- CUDA9.0
- 황토펜션
- cuda
- ubuntu
- 오봉 계곡
- 지리산 황토 펜션
- LISP
- 파이썬 GUI
- LISP 함수
- 계곡 펜션
- 전용 계곡
- 파이썬 yield
- 출근 전날
- 지리산 펜션
- anaconda tensorflow
- tensorflow 설치
- 하늘숲 황토 펜션
- tensorflow
- machine learning
- 인공지능
- python yield
- 파이썬
- Python
- 아나콘다 텐서플로
Archives
- Today
- Total
YongWook's Notes
<파이썬> Tkinter - GUI 예제 본문
파이썬의 Tkinter를 이용하여 GUI(Graphic User Interface)를 만드는 예제를 공부해보고 그 과정에서 습득한 지식들을 정리해본다.
- 레스토랑 메뉴판 예제
https://onedrive.live.com/redir?resid=4FEDA092E8180CD3!9073&authkey=!ADUngajkMjkd0Eg&ithint=file%2czip123456789101112131415#레스토랑 메뉴판 예제에 사용된 개념 Tips
#조환규는 학과 교수님 성함입니다..
#뱀 버튼을 누르고 주방장에게 "내가 당신에게 A+를 주겠소."를 입력하면 10% 할인을 받을 수 있다.
root
=
Tk()
root.geometry(
"600x800+100+100"
)
# root의 GUI창이 생성되는 위치 지정. (크기+x좌표+y좌표)
popup
=
Toplevel(root)
# root위에 다른 창을 띄우고 싶을 때는 Tk()를 한번 더 쓰면 안되고 Toplevel()을 쓴다.
Radiobutton(popup, text
=
"라디오버튼"
, variable
=
self
.var, value
=
i,
indicatoron
=
0
, relief
=
RAISED, command
=
self
.selected).pack(fill
=
X)
# http://www.tutorialspoint.com/python/tk_radiobutton.htm <<- radio버튼 설명.
# indicatoron =0 은 라디오버튼에 있는 동그란 체크박스를 숨겨준다.
# relief는 해당 element 전체의 모양타입을 정해줄 수 있음
- N각형 그리기
123456789101112131415161718192021222324252627
from
Tkinter
import
*
import
math
root
=
Tk()
root.title(u
"간단하게 선그리기"
)
N
=
400
#canvas의 크기
mypaper
=
Canvas(root, width
=
N,height
=
N, background
=
"light green"
)
def
draw_N(ox,oy,r,n):
#ox,oy는 원점좌표, r은 반지름, n은 n각형
mypaper.create_text(ox,oy,font
=
(u
"맑은고딕"
,
20
), fill
=
"blue"
,text
=
str
(n)
+
u
"각형"
)
total_angle_sum
=
(n
-
2
)
*
math.pi
#N각형에서 내각의 총합
a
=
ox
+
r
b
=
oy
for
i
in
range
(n):
#기준점(a,b)에서 삼각함수로 다음 점을 계산하여 서로 잇는다.
mypaper.create_line(a,b,ox
+
r
*
math.cos((i
+
1
)
*
(math.pi
-
total_angle_sum
/
n)),
oy
+
r
*
math.sin((i
+
1
)
*
(math.pi
-
total_angle_sum
/
n)),width
=
3
)
#a, b를 갱신
a
=
ox
+
r
*
math.cos((i
+
1
)
*
(math.pi
-
total_angle_sum
/
n))
b
=
oy
+
r
*
math.sin((i
+
1
)
*
(math.pi
-
total_angle_sum
/
n))
draw_N(
200
,
200
,
100
,
10
)
#10각형
mypaper.grid(row
=
0
,column
=
0
)
root.mainloop()
- &그리기(곡선)
123456789101112131415
root
=
Tk()
root.title(u
"&(and)기호 그리기"
)
cw
=
500
ch
=
500
canvas_1
=
Canvas(root, width
=
cw, height
=
ch, background
=
"white"
)
canvas_1.pack()
Q
=
[
430
,
300
,
315
,
400
,
175
,
400
,
151
,
300
,
300
,
180
,
325
,
70
,
200
,
35
,
180
,
120
,
430
,
385
]
canvas_1.create_line(Q,smooth
=
'true'
,dash
=
(
4
))
root.mainloop()
그림판에 손으로 그림을 그리고 좌표를 따라가면서 지정해주면 쉽다. - sin감쇠 그래프 그리기
123456789101112131415161718192021222324252627282930313233343536373839404142
from
Tkinter
import
*
import
math
root
=
Tk()
width
=
360
height
=
300
vcenter
=
height
/
2
hcenter
=
width
/
2
y_amplitude
=
100
#sin함수의 최대값은 1이기 때문에 그림으로 나타내기 위해 증폭
#Canvas 생성
canvas
=
Canvas(root, width
=
width, height
=
height, bg
=
'white'
)
canvas.pack(side
=
'left'
)
cycle
=
180
# sin()감쇠
px
=
0
py
=
vcenter
for
x
in
range
(
1000
):
y
=
math.sin((x
*
math.pi)
/
cycle)
*
y_amplitude
+
vcenter
sinLine
=
canvas.create_line(px, py, x, y, fill
=
'red'
)
px
=
x
py
=
y
if
(cycle>
1
):
cycle
-
=
0.5
if
(y_amplitude>
1
):
y_amplitude
-
=
0.3
#좌표축 생성
canvas.create_line(hcenter,
0
, hcenter, height)
canvas.create_line(
0
, vcenter, width, vcenter)
#Text 출력
canvas.create_text(
100
,
10
, text
=
u
"감쇠하는 모양"
, fill
=
'red'
)
root.title( u
" 함수 커브 그리기"
)
root.mainloop()
함수로 표현이 가능한 그래프는 for반복문으로 처리하자. - 벽돌 담 그리기
https://onedrive.live.com/redir?resid=4FEDA092E8180CD3!9160&authkey=!AHmqElkUtPwMlMA&ithint=file%2czip
123456789101112131415161718def
curpos(event) :
global
x, y, item
x
=
event.x
y
=
event.y
item
=
canvas.find_closest(x,y)
#클릭한 마우스의 좌표와 가장 가까운 아이템 찾기
def
moveitem(event) :
#선택된 아이템을 이동시키는 함수 -> 드래그&드랍
global
x, y
nx
=
event.x
ny
=
event.y
distx
=
nx
-
x
disty
=
ny
-
y
canvas.move(item, distx, disty)
x
=
nx
y
=
ny
canvas.bind(
"<Button-1>"
, curpos)
#한번 클릭
canvas.bind(
"<B1-Motion>"
, moveitem)
#클릭한 다음 이동(드래그)
벽돌 색깔과 개구리의 위치는 랜덤으로 생성되며 벽돌과 개구리 object를 drag&drop 방식으로 옮길 수 있다.
'-software > python' 카테고리의 다른 글
<WebGL> 그림판 만들기 (0) | 2016.09.30 |
---|---|
<파이썬> 파일명, 경로 변경하기 (0) | 2016.08.26 |
<파이썬> random과 배열, 생태계 개체수 예측하기 (0) | 2016.05.09 |
<파이썬> TKinter - GUI 만들기 (2) | 2016.04.25 |
<파이썬> 비트 연산자 (bit operation) 및 예제 (0) | 2016.04.21 |