【问题标题】:Connect wxpython TextCtrl with python script input()将 wxpython TextCtrl 与 python 脚本 input() 连接
【发布时间】:2021-01-22 06:08:09
【问题描述】:

我正在开发 python GUI,它可以加密用户的输入并将加密的消息存储在一个新文件中。我在尝试将 python 脚本中的 input() 实现到 wxpython GUI 时遇到问题。将不胜感激。

下面是我的python加密脚本(公钥和私钥是从另一个.py文件生成的):

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP

data = input("Enter message: ").encode("utf-8")
file_out = open("encrypted_data.bin", "wb")

recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = get_random_bytes(16)

# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
enc_session_key = cipher_rsa.encrypt(session_key)

# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
file_out.close()

这是我目前在 wxpython GUI 上的工作:

import wx
import subprocess
import os

class MyFrame(wx.Frame):    
    def __init__(self, parent, title):
        super().__init__(parent, title=title)
        self.panel = MyPanel(self)

class MyPanel(wx.Panel):
    def __init__(self, parent): 
        super(MyPanel,self).__init__(parent)
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)

        self.genBtn = wx.Button(self, label = "Generate Keys")
        hbox.Add(self.genBtn, wx.EXPAND)
        self.genBtn.Bind(wx.EVT_BUTTON, self.genProc)
        self.SetSizer(hbox)

        self.sendBtn = wx.Button(self, label="Send Message")
        hbox.Add(self.sendBtn, wx.BOTTOM)
        self.genBtn.Bind(wx.EVT_BUTTON, self.genProc)
        self.SetSizer(hbox)


    def genProc(self, event):
        p = subprocess.Popen(["python", "-u", "Generate.py"], stdout=subprocess.PIPE, bufsize=-1)
        self.pid = p.pid
        wx.MessageBox("Public and Private Keys Generated", 'Dialog', wx.OK | wx.ICON_INFORMATION)
        #wx.MessageBox("Message Box Icon Warning", 'Dialog', wx.OK | wx.ICON_WARNING)
        #wx.MessageBox("Message Box Dialog Error", 'Dialog', wx.OK | wx.ICON_ERROR)
        
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(parent=None, title="Sender")
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True
        
def main():
    app = MyApp()
    app.MainLoop()

if __name__ == '__main__':
    main()

【问题讨论】:

  • 所以您希望用户通过 wx.TextCtrl() 发送一些输入?
  • 是的,可以用简单的 wxpython 代码吗?
  • 您可能还希望从Generate.py 的命令行中接受密码。对于import sys 并使用myargs = sys.argv[1:] 之类的东西,它将给出您传递给Generate.py 的参数的list。安全说明:参数在 clear 中传递。

标签: python wxpython


【解决方案1】:

嗯,你需要的是一个简单的绑定:

self.yourtestctrl.Bind(wx.EVT_TEXT_ENTER, self.OnTextEnter)

注意:

为此,您需要将wx.TE_PROCESS_ENTER 添加到您的文本控件的样式中

完成这个你必须创建你的函数:

def OnTextEnter(self, event):
    
    text = [self.yourtextevetn.GetLineText(line)for line in self.yourtextctrl.GetNumberOfLines()]

    text = "\n".join("text")

    #here you have your input and you can store it o call a function with it

注意:

如果您希望用户在需要时发送输入,您可以创建一个wx.Button,然后将其绑定到上述函数

【讨论】:

  • 什么是 self.yourtextevetn?我已经尝试使用代码,但似乎我仍然坚持如何将输入数据导入脚本
  • 您必须将 yourtextctrl 替换为存储 wx.TextCtrl 的变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多