wxPython TextCtrl转换为UPPERCASE

时间:2009-08-06 22:47:10

标签: fonts wxpython uppercase textctrl

我一直在寻找一种方法,将输入textctrl字段的文本转换为大写。我尝试使用验证器并使用'ord'将键事件对象的值转换为大写字母char值但是我找不到任何'set'方法。我可能能够直接访问事件类变量,因为python中的类变量从来都不是私有的,但我很难找到我需要的确切信息。

无论如何,这似乎是一种过于复杂的方法。有没有人有任何建议。我错过了一些明显的东西吗?

2 个答案:

答案 0 :(得分:3)

试试这个。

import wx

class UpperTextCtrl(wx.TextCtrl):
    def __init__(self, *args, **kwargs):
        super(UpperTextCtrl, self).__init__(*args, **kwargs)
        self.Bind(wx.EVT_TEXT, self.on_text)
    def on_text(self, event):
        event.Skip()
        selection = self.GetSelection()
        value = self.GetValue().upper()
        self.ChangeValue(value)
        self.SetSelection(*selection)

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None, -1, 'Uppercase Text Control')
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        text = UpperTextCtrl(panel, -1, style=wx.TE_MULTILINE)

        # change the font
        font = text.GetFont()
        font.SetPointSize(12)
        text.SetFont(font)

        sizer.Add(text, 1, wx.EXPAND|wx.ALL, 10)
        panel.SetSizer(sizer)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Show()
    app.MainLoop()

答案 1 :(得分:1)

检查此解决方案。

import wx
import string

class UpperTextCtrl(wx.TextCtrl):
    def __init__(self, *args, **kwargs):
        super(UpperTextCtrl, self).__init__(*args, **kwargs)
        self.Bind(wx.EVT_CHAR, self.on_char)
    def on_char(self, event):
        key=event.GetKeyCode()
        text_ctrl=event.GetEventObject()
        if chr(key) in string.letters:
            text_ctrl.AppendText(chr(key).upper())
            return
        event.Skip()

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None, -1, 'Uppercase Text Control')
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        text = UpperTextCtrl(panel, -1, style=wx.TE_MULTILINE)

        # change the font
        font = text.GetFont()
        font.SetPointSize(12)
        text.SetFont(font)

        sizer.Add(text, 1, wx.EXPAND|wx.ALL, 10)
        panel.SetSizer(sizer)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Show()
    app.MainLoop()