从kivy中的弹出窗口检索文本输入

retrieving text input from popup in kivy

我复制了一些代码,使弹出窗口出现在我的程序的屏幕上,弹出窗口有一个文本框。我想将其中的文本作为参数传递给函数,但我在这样做时遇到了一些麻烦。 .text 属性 不是一个选项,因为弹出窗口是在 class 方法中声明的,我不知道如何定义一个与我的主应用程序中的实例行为相同的弹出窗口 class. 主程序:

import os
import cryptography
from cryptography.fernet import Fernet


def gen_key():
    key = Fernet.generate_key()
    with open("new_key", "w") as f:
        f.write("SAVE THIS KEY TO DECRYPT DATA\nDO NO SHARE THIS KEY WITH ANYONE\n==============================================\n")
        f.write(key.decode("ASCII"))
    return key


#secret_key = gen_key()

def encrypt(filename, key):
    """
    Given a filename (str) and key (bytes), it encrypts the file and write it
    """
    f = Fernet(key)
    with open(filename, "rb") as file:
        # read all file data
        file_data = file.read()
    # encrypt data
    encrypted_data = f.encrypt(file_data)
    # write the encrypted file
    with open(filename, "wb") as file:
        file.write(encrypted_data)


def decrypt(filename, key):
    """
    Given a filename (str) and key (bytes), it decrypts the file and write it
    """
    #key = input("enter key: ")
    f = Fernet(key)
    with open(filename, "rb") as file:
        # read the encrypted data
        encrypted_data = file.read()
    # decrypt data
    decrypted_data = f.decrypt(encrypted_data)
    # write the original file
    with open(filename, "wb") as file:
        file.write(decrypted_data)


def main(directory, option, secret_key):

    # assign directory
    # directory = input("Enter path pls: ") | taken as parameter
    # option = input("(E)ncryption or (D)ecryption?: ")  | taken as parameter
    # iterate over files that directory
    if option.lower() == "e":
        #secret_key = input("Enter key to encrypt with: ")
        for filename in os.listdir(directory):
            f = os.path.join(directory, filename)
            if os.path.isfile(f): # checking if it is a file
                encrypt(f, secret_key)
    elif option.lower() == "d":
        #secret_key = input("Enter key to decrypt with: ")
        for filename in os.listdir(directory):
            f = os.path.join(directory, filename)
            if os.path.isfile(f): # checking if it is a file
                decrypt(f, secret_key)


if __name__ == "__main__":
    main()

kivy 文件

import fix_proj
import kivy
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup


class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 2

        self.gen_key = Button(text="Generate Key: ",
                              font_size=30, on_press=self.gen_key_press)
        self.key_output = TextInput()

        self.add_widget(self.gen_key)
        self.add_widget(self.key_output)

        self.encrypt = Button(text="Encrypt Directory: ",
                              font_size=30, on_press=self.encrypt_popup_key)
        self.encrypted_dir = TextInput()
        self.add_widget(self.encrypt)
        self.add_widget(self.encrypted_dir)

        self.decrypt = Button(text="Decrypt Directory: ", font_size=30, on_press = self.decrypt_popup_key)
        self.decrypted_dir = TextInput()
        self.add_widget(self.decrypt)
        self.add_widget(self.decrypted_dir)

    def gen_key_press(self, event):
        self.key_output.text = fix_proj.gen_key()

    def encrypt_popup_key(self, event):
        layout = GridLayout(cols = 1, padding = 10)
  
        key_input = TextInput(text = "")
        closeButton = Button(text = "Apply")
  
        layout.add_widget(key_input)
        layout.add_widget(closeButton)       
  
        # Instantiate the modal popup and display
        popup = Popup(title ='Enter key:',
                      content = layout,
                      size_hint =(None, None), size =(200, 200))  
        popup.open()   
  
        # Attach close button press with popup.dismiss action
        closeButton.bind(on_press = self.enc_dir_input)
        #popup.dismiss()

    def decrypt_popup_key(self, event):
        layout = GridLayout(cols = 1, padding = 10)
  
        key_input = TextInput(text = "")
        closeButton = Button(text = "Apply")
  
        layout.add_widget(key_input)
        layout.add_widget(closeButton)       
  
        # Instantiate the modal popup and display
        popup = Popup(title ='Enter key:',
                      content = layout,
                      size_hint =(None, None), size =(200, 200))  
        popup.open()   
  
        # Attach close button press with popup.dismiss action
        closeButton.bind(on_press = self.dec_dir_input)
        #popup.dismiss()

    # TODO bind it to its respective button and pass params accordingly
    def enc_dir_input(self, dir):
        fix_proj.main(self.encrypted_dir.text, 'e', self.encrypt_popup_key.key_input.text)

    # TODO bind it to its respective button and pass params accordingly
    def dec_dir_input(self, dir):
        fix_proj.main(self.decrypted_dir.text, 'd', self.decrypt_popup_key.key_input.text)


class Encryptor(App):
    def build(self):
        return MyGrid()


if __name__ == "__main__":
    Encryptor().run()

下面是您的方法 enc_dir_input 的实现。其他类似。

首先通过key_input.text通过closeButton.on_press

closeButton.bind(on_press = lambda *args : self.enc_dir_input(key_input.text))

现在以所需的方法访问此文本,

    def enc_dir_input(self, text):
        fix_proj.main(self.encrypted_dir.text, 'e', text)