wxPython TextCtrl 转换为大写

发布于 2024-07-30 14:12:00 字数 221 浏览 5 评论 0原文

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

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

I was looking for a way to transform text entered into the textctrl field to uppercase. I tried using a validator and transforming the value of the key event object to the uppercase letter char value using 'ord' however I could not find any 'set' methods. I may be able to access the event class variable directly as class variables in python are never really private but im having trouble finding the exact information I need to do that.

Anyway this seems like an overly complicated approach. Does anyone else have any suggestions. Am I missing something obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光倒影 2024-08-06 14:12:00

尝试这个。

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()

Try this.

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()
旧夏天 2024-08-06 14:12:00

检查这个解决方案。

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()

Check this solution.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文