顯示具有 wxWidgets 標籤的文章。 顯示所有文章
顯示具有 wxWidgets 標籤的文章。 顯示所有文章

2012年8月23日 星期四

2012年8月21日 星期二

python wxwidgets html

import wx
import wx.html

if __name__ == "__main__" :
    app = wx.App()

    frame = wx.Frame(None)
   
    htmlwin = wx.html.HtmlWindow(frame)
    htmlwin.SetPage("

Hello, World !

")

    frame.Show()
   
    app.MainLoop()




Reference :
wxHTML - wxPyWiki

2012年6月14日 星期四

Python wxwidgets bind key 'Ctrl + C'

處理原生 EVT_KEY_DOWN 事件
    self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)

    def on_key_down(self, event):
        """Key down event handler."""
        key = event.KeyCode()
       
        controlDown = event.ControlDown()
        altDown = event.AltDown()
        shiftDown = event.ShiftDown()

        # Cut to the clipboard.
        if (controlDown and key in (ord('C'), ord('c'))):
            copy()
        # Insert the next command from the history buffer.
        else:
            event.Skip()

Reference :
CharacterCodesAndKeyboards - wxPyWiki


使用 wx_tools class
import wx_tools
class Frame(wx.Frame):
    def __init__(self, *args, **keywords):
       self.__init_key_handlers()

    def __init_key_handlers(self):
        '''Initialize key shortcuts.'''
  
        self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
 
        def copy():
            print 'copy'

        self.key_handlers = {
            Key(ord('C'), cmd=True): copy,
         }

    def on_key_down(self, event):
        '''wx.EVT_KEY_DOWN handler.'''
        key = Key.get_from_key_event(event)
        handler = self.key_handlers.get(key, None)
        if handler:
            handler()
        else:
            event.Skip()

Reference :
Nullege: A Search Engine for Python source code
garlicsim_wx.widgets.workspace_widgets.crunching_controls.step_profiles_controls.step_profiles_list.step_profiles_list :: garlicsim_wx 0.6.3 : PyDoc.net

2012年6月13日 星期三

python wxwidgets 捕捉 Alt+F4

1. 捕捉 wxwidgets 窗口關閉事件
    self.frame.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, evt):
        print 'cannot close'

#        dlg = wx.MessageDialog(None, "Is this explanation OK?",
#                'A Message Box',
#                wx.YES_NO | wx.ICON_QUESTION)
#        retCode = dlg.ShowModal()
#        if (retCode == wx.ID_YES):
#            self.frame.Destroy()
#            print "yes"
#        else:
#            print "no"

2. 捕捉 wx 快捷鍵 (wx.EVT_HOTKEY) (不能使用 wx.EVT_KEY_DOWN 事件捕捉快捷鍵)
 import win32con  #for the VK keycodes
 
    def regHotKey(self):
        self.hotKeyId = 100
        self.frame.RegisterHotKey(
            self.hotKeyId, #a unique ID for this hotkey
            win32con.MOD_CONTROL + win32con.MOD_SHIFT , #the modifier key
            win32con.VK_DELETE) #the key to watch for

        self.regHotKey()
        self.Bind(wx.EVT_HOTKEY, self.OnClose, id=self.hotKeyId)

3. 捕捉系統快捷鍵
Tim Golden's Python Stuff: Catch system-wide hotkeys




Reference :
关于wxWidgets中的RegisterHotKey不得不说的故事 - 程序、程序及其他 - 博客频道 - CSDN.NET
RegisterHotKey - wxPyWiki
使用热键控制python程序 - Stonelee's Blog
Python - Dictionary full of windows keys


Python wxwidgets bind key 做法 class 版

wx_tools.py
class Key(object):
    '''A key combination.'''

    def __init__(self, key_code, cmd=False, alt=False, shift=False):

        self.key_code = key_code
        '''The numerical code of the pressed key.'''

        self.cmd = cmd
        '''Flag saying whether the ctrl/cmd key was pressed.'''

        self.alt = alt
        '''Flag saying whether the alt key was pressed.'''

        self.shift = shift
        '''Flag saying whether the shift key was pressed.'''

    @staticmethod
    def get_from_key_event(event):
        '''Construct a Key from a wx.EVT_KEY_DOWN event.'''
        return Key(event.GetKeyCode(), event.CmdDown(),
                   event.AltDown(), event.ShiftDown())

    def __hash__(self):
        return hash(tuple(sorted(tuple(vars(self)))))

    def __eq__(self, other):
        if not isinstance(other, Key):
            return NotImplemented
        return self.key_code == other.key_code and \
            self.cmd == other.cmd and \
            self.shift == other.shift and \
            self.alt == other.alt


 main.py
from wx_tools import Key

class MyApp(wx.App):

    def __init_key_handlers(self):
        '''Initialize key shortcuts.'''

        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        # Now you define handlers for various keys:

        def bypass_key():
            print 'key bypassed'

        # This is the big dict that maps keys to their handlers:

        self.key_handlers = {
            Key(wx.WXK_ESCAPE): bypass_key,
            Key(wx.WXK_DELETE): bypass_key,
        }

    def init_frame(self):
        self.res = xrc.XmlResource('login.xrc')

        self.frame = self.res.LoadFrame(None, 'mainFrame')

        # bind key event
        self.__init_key_handlers()

        self.frame.Show()

    def OnKeyDown(self, event):
        '''wx.EVT_KEY_DOWN handler.'''
        key = Key.get_from_key_event(event)
        handler = self.key_handlers.get(key, None)
        if handler:
            handler()
        else:
            event.Skip()




Reference :
Processing key events in a strategy pattern - wxPyWiki

Python wxwidgets bind key 做法

class MyApp(wx.App):
    def init_frame(self):
        self.res = xrc.XmlResource('login.xrc')

        self.frame = self.res.LoadFrame(None, 'mainFrame')

        # 在此 frame 下監聽所有按鍵反應
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        # 或綁定單一原件
        wx.EVT_KEY_DOWN(xrc.XRCCTRL(self.frame, 'userTxt'), self.OnKeyDown)

        self.frame.Show()

    def OnKeyDown(self, e):
        key = e.GetKeyCode()

        if key == wx.WXK_ESCAPE:
            print 'Event!'
        else:
            e.Skip()


用陣列綁定多個 key
python - In wxPython how do you bind a EVT_KEY_DOWN event to the whole window? - Stack Overflow




Reference :
python - wxpython capture keyboard events in a wx.Frame - Stack Overflow
Events in wxPython

self.Bind vs. self.button.Bind - wxPyWiki

Keycode Table

CharacterCodesAndKeyboards - wxPyWiki

wxPython: Catching Key and Char Events « The Mouse Vs. The Python

2012年6月12日 星期二

wxWidgets layout 工具

DialogBlocks

wxDesigner (nonfree)

wxFormBuilder (64-bit)








VisualWx


spe (Python IDE, 整合 pychecker & xrced)



wxGlade(free)
boa-constructor(free),只支持wxPython 2.4.3
xrced(in wxPython docs, demos and tools),只能编辑xrc文件
DialogBlocks(commercial),不支持python代码生成,只能生成xrc文件
wxDesigner(commercial)














Reference :

[python-chinese] 有没有wxPython的对话框设计器?