Wxpython - TextCtrl 问题/奇怪的复制“bug” (?)

发布于 2024-12-13 03:38:13 字数 2228 浏览 0 评论 0原文

我有一个奇怪的问题。我只能将文本复制一次。如果我按清除并再次输入,文本就会消失,我必须重新启动它。我哪里搞砸了?

现在的问题是:

1)我可以将清除功能绑定到“加密/解密”,以便每次我想再次输入它吗 自动清除?

2)如何使用按钮将输出复制到剪贴板?

代码(相当混乱):

import wx

class main(wx.Frame):



    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Title',size=(353,270))

        self.panel=wx.Panel(self)

        button1=wx.Button(self.panel,label='Encrypt\Decrypt',pos=(10,10),size=(-1,60))
        button2=wx.Button(self.panel,label='Current PIN',        pos=(150,10),size=(-1,60))
        button3=wx.Button(self.panel,label='Change PIN',           pos=(250,10),size=(-1,60))
        button4= wx.Button(self.panel,label='Reset',pos=(10,200),size=(-1,20))

        self.Bind(wx.EVT_BUTTON, self.option1, button1)
        self.Bind(wx.EVT_BUTTON, self.option2, button2)
        self.Bind(wx.EVT_BUTTON, self.option3, button3)
        self.Bind(wx.EVT_BUTTON, self.clear,  button4)

        self.Bind(wx.EVT_CLOSE,self.closewindow)
        self.pin='ABC'

    def option1(self,event):
        box1=wx.TextEntryDialog(None,'Type...','','...here')
        if box1.ShowModal()==wx.ID_OK:
            answer1=box1.GetValue()

        def xor(text, key):
            list = ''
            for char in text:
                for ch in key:
                    char = chr(ord(char) ^ ord(ch))
                list += char
            return list

        msg = xor(answer1, self.pin)
        self.output=wx.TextCtrl(self.panel, -1,msg,pos=(10,80),size=(300, 100), style=wx.TE_MULTILINE|wx.TE_READONLY)
        #output=wx.StaticText(self.panel,-1,msg,(10,80),(260,-1),wx.ALIGN_CENTER)


    def option2(self,event):
        box2=wx.MessageDialog(None,self.pin,'Current PIN',wx.OK)
        answer2=box2.ShowModal()
        box2.Destroy()

    def option3(self,event):
        box3=wx.TextEntryDialog(None,'Type...','','...here')
        if box3.ShowModal()==wx.ID_OK:
            answer3=box3.GetValue()
            self.pin=answer3

    def closewindow(self,event):
        self.Destroy()

    def clear(self,event):
        self.output.Clear()






if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=main(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

I have a weird problem. I can only copy the text once. If I press clear and type again the text disappears and I have to restart it. Where have I messed up?

Now the questions:

1) Can I bind the clear function to the "Encrypt/Decrypt" so that each time I want to type again it
clears automatically?

2)How can I copy the output to the clipboard with a button?

Code(Pretty messy):

import wx

class main(wx.Frame):



    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Title',size=(353,270))

        self.panel=wx.Panel(self)

        button1=wx.Button(self.panel,label='Encrypt\Decrypt',pos=(10,10),size=(-1,60))
        button2=wx.Button(self.panel,label='Current PIN',        pos=(150,10),size=(-1,60))
        button3=wx.Button(self.panel,label='Change PIN',           pos=(250,10),size=(-1,60))
        button4= wx.Button(self.panel,label='Reset',pos=(10,200),size=(-1,20))

        self.Bind(wx.EVT_BUTTON, self.option1, button1)
        self.Bind(wx.EVT_BUTTON, self.option2, button2)
        self.Bind(wx.EVT_BUTTON, self.option3, button3)
        self.Bind(wx.EVT_BUTTON, self.clear,  button4)

        self.Bind(wx.EVT_CLOSE,self.closewindow)
        self.pin='ABC'

    def option1(self,event):
        box1=wx.TextEntryDialog(None,'Type...','','...here')
        if box1.ShowModal()==wx.ID_OK:
            answer1=box1.GetValue()

        def xor(text, key):
            list = ''
            for char in text:
                for ch in key:
                    char = chr(ord(char) ^ ord(ch))
                list += char
            return list

        msg = xor(answer1, self.pin)
        self.output=wx.TextCtrl(self.panel, -1,msg,pos=(10,80),size=(300, 100), style=wx.TE_MULTILINE|wx.TE_READONLY)
        #output=wx.StaticText(self.panel,-1,msg,(10,80),(260,-1),wx.ALIGN_CENTER)


    def option2(self,event):
        box2=wx.MessageDialog(None,self.pin,'Current PIN',wx.OK)
        answer2=box2.ShowModal()
        box2.Destroy()

    def option3(self,event):
        box3=wx.TextEntryDialog(None,'Type...','','...here')
        if box3.ShowModal()==wx.ID_OK:
            answer3=box3.GetValue()
            self.pin=answer3

    def closewindow(self,event):
        self.Destroy()

    def clear(self,event):
        self.output.Clear()






if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=main(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

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

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

发布评论

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

评论(1

一瞬间的火花 2024-12-20 03:38:13

每次按下按钮(方法option1)时,您都会创建一个重叠的TextCtrl。您应该在构造函数中创建并仅更改方法中的文本。

1) 只需在 option1 方法的开头调用 self.output.Clear() 即可。

2)

clipdata = wx.TextDataObject()
clipdata.SetText("Text here")
wx.TheClipboard.Open()
wx.TheClipboard.SetData(clipdata)
wx.TheClipboard.Close()

Every time you push the button (method option1) you create a new overlapping TextCtrl. You should have created in the constructor and just change the text in the method.

1) Just call self.output.Clear() at the beginning of the option1 method.

2)

clipdata = wx.TextDataObject()
clipdata.SetText("Text here")
wx.TheClipboard.Open()
wx.TheClipboard.SetData(clipdata)
wx.TheClipboard.Close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文