如何让 wxpython 密码 textctrl 显示字符? [英] how to make wxpython password textctrl show chars?

查看:29
本文介绍了如何让 wxpython 密码 textctrl 显示字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 wxPython 可以创建一个密码字段:

With wxPython a password field could be created as:

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

我想知道是否有办法动态地将此密码字段更改为普通的 textctrl,以便用户可以看到密码是什么.

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.

推荐答案

创建控件后无法更改样式标志.

Its not possible to change the style flag after creating the control.

您可以销毁控件并创建一个没有密码标志的新控件,或者在 sizer 中并排保留两个控件,一个始终隐藏.当你想切换时,你可以将文本复制到另一个文本控件中,隐藏一个并显示另一个,然后在 sizer 上调用 Layout.

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. 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.

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

这篇关于如何让 wxpython 密码 textctrl 显示字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆