简体   繁体   English

如何制作wxpython密码textctrl show chars?

[英]how to make wxpython password textctrl show chars?

With wxPython a password field could be created as: 使用wxPython,可以将密码字段创建为:

wx.TextCtrl(frm, -1, '', style=wx.TE_PASSWORD )

I'm wondering if there is a way to dynamically change this password field into a normal textctrl, such that user could see what the password is. 我想知道是否有办法将此密码字段动态更改为普通的textctrl,以便用户可以看到密码是什么。

Its not possible to change the style flag after creating the control. 创建控件后无法更改样式标志。

You can either destroy the control and create a new one without the password flag, or maintain two side by side in a sizer, with one always hidden. 你可以破坏控件并创建一个没有密码标志的新控件,或者在sizer中并排保持两个,一个总是隐藏。 When you want to switch you can copy the text into the other text control, Hide one and Show the other then call Layout on the sizer. 如果要切换,可以将文本复制到另一个文本控件中,隐藏一个并显示另一个,然后在sizer上调用Layout。

import wx

class Frame(wx.Frame):
    def __init__(self,*args,**kwargs):
        wx.Frame.__init__(self,*args,**kwargs)
        panel= wx.Panel(self)
        self.password_shown= False

        sizer= wx.BoxSizer(wx.VERTICAL)
        self.password_sizer= wx.BoxSizer(wx.HORIZONTAL)
        self.text_password= wx.TextCtrl(panel,style=wx.TE_PASSWORD)
        self.password_sizer.Add(self.text_password,0,wx.ALL,5)
        self.text_no_password= wx.TextCtrl(panel)
        self.text_no_password.Hide()
        self.password_sizer.Add(self.text_no_password,0,wx.ALL,5)
        sizer.Add(self.password_sizer)
        self.button= wx.Button(panel,-1,"Toggle Password")
        sizer.Add(self.button,0,wx.ALL,5)
        self.button.Bind(wx.EVT_BUTTON,self.OnButton)
        panel.SetSizer(sizer)

        self.Show()

    def OnButton(self,event):
        self.text_password.Show(self.password_shown)
        self.text_no_password.Show(not self.password_shown)
        if not self.password_shown:
            self.text_no_password.SetValue(self.text_password.GetValue())
            self.text_no_password.SetFocus()
        else:
            self.text_password.SetValue(self.text_no_password.GetValue())
            self.text_password.SetFocus()
        self.text_password.GetParent().Layout()
        self.password_shown= not self.password_shown


if __name__ == "__main__":
    app= wx.App(0)
    Frame(None)
    app.MainLoop()

I'm not aware of a way to dynamically change the style flags on the text control widget after creation. 我不知道在创建后动态更改文本控件窗口小部件上的样式标志的方法。 Some widgets allow this sort of thing on some OSes and some do not. 有些小部件在某些操作系统上允许这种情况,有些则不允许。 You could just create two text controls with the second one in normal mode and hide it. 您可以在正常模式下创建两个文本控件,第二个文本控件将其隐藏。 Then when you want to toggle, you grab the password-protected version's value and hide it, give the value to the normal one and show it. 然后,当您想要切换时,您将获取受密码保护的版本的值并将其隐藏,将值提供给正常版本并显示它。 You'll probably need to call Layout() at the end as well. 您可能还需要在末尾调用Layout()。

then it would not be a password entry, but you can use style=wx.TE_MULTILINE or TE_RICH. 那么它不是密码输入,但你可以使用style = wx.TE_MULTILINE或TE_RICH。 if that is what you are asking. 如果这就是你要问的。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM