Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

Connect wxpython TextCtrl with python script input()

I'm working on python GUI that can encrypt user's input and storing the encrypted message in a new file. I have problem trying to implement input() from python script into wxpython GUI. Would appreciate any help.

Below is my python script for encryption (public and private key is generated from another .py file):

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

And this is my current working on 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()
question from:https://stackoverflow.com/questions/65840099/connect-wxpython-textctrl-with-python-script-input

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Weell, what you need is a semple binding:

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

Note:

to do this you need to add wx.TE_PROCESS_ENTER to yourtextcontrol's style

done this you have to create your function:

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

    text = "
".join("text")

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

Note:

if you want the user to send the input when he wants you can create a wx.Button and then bind it to the above function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...