wxPython

1、子线程和主线程通信
安装库:pip3 install pypubsub

from pubsub import pub
class TestFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.button_test = wx.Button(self.panel, label=u"测试", size=(150, 30))
        #订阅消息event_type_test
        pub.subscribe(self.event_handle, "event_type_test")
    
    #消息处理函数,
    def event_handle(self, msg):
        logger.info("msg=" + str(msg))

#子线程中发送消息
pub.sendMessage("event_type_test", msg="hello zsg")

2、debug调试时,如果要进入第三方库的代码需要在launch.json中设置justMyCode为false:

    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false
        }
    ]

3、限制TextCtrl中输入的字符

class NumberValidator(wx.Validator):# 创建验证器子类      
    def __init__(self):
        wx.Validator.__init__(self)
        self.ValidInput = ['.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        self.StringLength = 0
        self.Bind(wx.EVT_CHAR, self.OnCharChanged)  #  绑定字符改变事件

    def OnCharChanged(self,event):
        # 得到输入字符的 ASCII 码
        keycode = event.GetKeyCode()
        # 退格(ASCII 码 为8),删除一个字符。
        if keycode == 8:
            self.StringLength -= 1
            #事件继续传递
            event.Skip()
            return

        # 把 ASII 码 转成字符
        InputChar = chr(keycode)

        if InputChar in self.ValidInput:
            # 第一个字符为 .,非法,拦截该事件,不会成功输入
            if InputChar == '.' and self.StringLength == 0:
                return False
            # 在允许输入的范围,继续传递该事件。
            else:
                event.Skip()
                self.StringLength += 1
                return True
        return False

    def Clone(self):
        return NumberValidator()

    def Validate(self, win):#1 使用验证器方法
        return True

    def TransferToWindow(self):
        return True

    def TransferFromWindow(self):
        return True

#创建TextCtrl时增加validator参数
self.tc_input = wx.TextCtrl(self.panel, -1, "666", validator=NumberValidator())

你可能感兴趣的:(wxPython)