본문 바로가기

Language/Python

[wxPython] wx.Button를 이용한 Button 이벤트 처리 와 랜덤 숫자 추출

본 내용의 출처는 이글루 블로그의 '하린아빠' 라는 네임을 쓰시는 분의 블로그입니다.

[출처] - http://pythondev.egloos.com/106147


행여나 이글루 블로그가 없어지면 참고할 곳이 사라지기에 주인장님께 댓글을 남기고 퍼 옵니다.




[wxPython] wx.Button를 이용한 Button 이벤트 처리 와 랜덤 숫자 추출


# -*- coding: cp949 -*-
#!/usr/bin/python

# buttons.py

import wx
# 랜덤 처리 유닛
import random


# Dialog 사이즈
APP_SIZE_X = 300
APP_SIZE_Y = 200


class MyButtons(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(APP_SIZE_X, APP_SIZE_Y))

       # 버튼 생성
        wx.Button(self, 1, 'Close', (50, 130))
        wx.Button(self, 2, 'Random Move', (150, 130), (110, -1))

        # 버튼 이벤트 함수 연결
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnRandomMove, id=2)

        self.Centre()
        self.ShowModal()
        self.Destroy()


   # 닫기 이벤트 처리 함수
    def OnClose(self, event):
        self.Close(True)


    # 랜덤 이동 이벤트 처리 함수
    def OnRandomMove(self, event):
        # 스크린 사이즈를 구함.
        screensize = wx.GetDisplaySize()
        # 랜덤하기 위치값을 구함
        randx = random.randrange(0, screensize.x - APP_SIZE_X) # X
        randy = random.randrange(0, screensize.y - APP_SIZE_Y) # Y
       # Frame 이동
        self.Move((randx, randy))


app = wx.App(0)
MyButtons(None, -1, 'buttons.py')
app.MainLoop()

[실행 화면]