在wxPython中动态构造TextCtrl的style属性

时间:2018-04-23 19:29:34

标签: wxpython

在创建文本控件时,我们通常使用一个或多个整数构造一个style属性,用OR(|)符号分隔:

control = wx.TextCtrl(self, id=wx.ID_ANY, style=wx.TE_PASSWORD | wx.TE_CENTER | wx.TE_READONLY)

我希望在实例化控件之后但在窗口中呈现之前,根据条件动态添加样式值。类似的东西:

if attr in dict_passwords:
    control.addStyle(wx.TE_PASSWORD)
if attr in dict_readonly:
    control.addStyle(wx.TE_READONLY)
...

有关如何实现这一目标的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:1)

至少有两种方法可以实现这一目标 您可以将属性预先分配给变量,也可以随意添加它们 预先分配:

import wx

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Variable TextCtrl style attributes')
        self.panel = wx.Panel(self)
        mystyle1 = wx.TE_READONLY
        mystyle2 = wx.TE_READONLY | wx.TE_CENTER
        mystyle3 = wx.TE_READONLY | wx.TE_PASSWORD
        mystyle4 = wx.TE_READONLY | wx.TE_CENTER | wx.TE_PASSWORD
        mystyle5 = wx.TE_PASSWORD
        tc0 = wx.TextCtrl(self.panel,-1,value="Ctrl 0",pos=(10,10),size=(350,20))
        tc1 = wx.TextCtrl(self.panel,-1,value="Ctrl 1",pos=(10,40),size=(350,20),style=mystyle1)
        tc2 = wx.TextCtrl(self.panel,-1,value="Ctrl 2",pos=(10,70),size=(350,20),style=mystyle2)
        tc3 = wx.TextCtrl(self.panel,-1,value="Ctrl 3",pos=(10,100),size=(350,20),style=mystyle3)
        tc4 = wx.TextCtrl(self.panel,-1,value="Ctrl 4",pos=(10,130),size=(350,20),style=mystyle4)
        tc5 = wx.TextCtrl(self.panel,-1,value="Ctrl 5",pos=(10,160),size=(350,20),style=mystyle5)
        self.Show()

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    app.MainLoop()

动态:

import wx

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Variable TextCtrl style attributes')
        self.panel = wx.Panel(self)
        tc0 = wx.TextCtrl(self.panel,-1,value="Ctrl 0",pos=(10,10),size=(350,20))
        tc1 = wx.TextCtrl(self.panel,-1,value="Ctrl 1",pos=(10,40),size=(350,20))
        tc1.SetWindowStyleFlag(wx.TE_READONLY)
        tc2 = wx.TextCtrl(self.panel,-1,value="Ctrl 2",pos=(10,70),size=(350,20))
        tc2.SetWindowStyleFlag(wx.TE_READONLY | wx.TE_CENTER)
        tc3 = wx.TextCtrl(self.panel,-1,value="Ctrl 3",pos=(10,100),size=(350,20))
        tc3.SetWindowStyleFlag(wx.TE_READONLY | wx.TE_RIGHT)
        tc4 = wx.TextCtrl(self.panel,-1,value="Ctrl 4",pos=(10,130),size=(350,20))
        tc4.SetWindowStyleFlag(wx.TE_READONLY | wx.TE_CENTER | wx.PASSWORD)
        tc5 = wx.TextCtrl(self.panel,-1,value="Ctrl 5",pos=(10,160),size=(350,20),style=wx.TE_PASSWORD)
        s = tc5.GetWindowStyleFlag()
        tc5.SetWindowStyleFlag(s+wx.TE_RIGHT)
        tc6 = wx.TextCtrl(self.panel,-1,value="Ctrl 6",pos=(10,190),size=(350,20))
        s = tc6.GetWindowStyleFlag()
        tc6.SetWindowStyleFlag(s+wx.TE_RIGHT)
        s = tc6.GetWindowStyleFlag()
        tc6.SetWindowStyleFlag(s+wx.TE_PASSWORD)

        self.Show()

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    app.MainLoop()

enter image description here

答案 1 :(得分:0)

在此发布我为此找到的解决方案,对于任何需要它的人: 我没有在实例化之后构建样式,而是使用我为此编写的函数,在此之前构建它。

def getControlStyle(attr):
    myStyle = 0
    if attr in globalvariables.list_olv_readonly:
        myStyle = myStyle | wx.TE_READONLY
    if attr in globalvariables.list_olv_multiline:
        myStyle = myStyle | wx.TE_MULTILINE
    if attr in globalvariables.list_olv_password:
        myStyle = myStyle | wx.TE_PASSWORD
    if attr in globalvariables.list_olv_aligncenter:
        myStyle = myStyle | wx.TE_CENTER
    if attr in globalvariables.list_olv_alignright:
        myStyle = myStyle | wx.TE_RIGHT
    return myStyle

现在,我按如下方式实例化控件:

controlStyle = getControlStyle(attr)
...
self.control = wx.TextCtrl(self, id=olv_dict[attr][1], value=str(attrDefaultValue), style=controlStyle)