如何在用户填写并单击注册按钮后重定向到另一个页面(例如main.py)并将其存储在使用kivy的python上的.ini本地数据库中

问题描述 投票:0回答:1

所以我不太擅长python,因为我来自php,我想开发这个应用程序,并且想使用本地存储来存储用户注册数据,注册后它们将被重定向到main.py页面,所以这里是注册的registration_screen.py页面代码及其代码带有有用的#comment以便于理解,请帮助我。

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDFlatButton
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.app import MDApp
from kivy.config import ConfigParser
import subprocess


# Define the registration screen
class RegistrationScreen(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # Create a vertical box layout for the registration screen
        layout = MDBoxLayout(orientation='vertical', padding=40, spacing=20)

        # Create text fields for username, email, and phone number
        self.username_field = MDTextField(hint_text="Username")
        self.email_field = MDTextField(hint_text="Email")
        self.phone_field = MDTextField(hint_text="Phone Number")

        # Create a button for registration
        register_button = MDFlatButton(text="Register", on_release=self.register)

        # Add widgets to the layout
        layout.add_widget(self.username_field)
        layout.add_widget(self.email_field)
        layout.add_widget(self.phone_field)
        layout.add_widget(register_button)

        # Add the layout to the screen
        self.add_widget(layout)

    # Method to handle registration process
    def register(self, *args):
        # Retrieve user input from text fields
        username = self.username_field.text
        email = self.email_field.text
        phone = self.phone_field.text

        # Store user registration data locally
        config = ConfigParser()
        config.read('registration.ini')

        if 'Registration' not in config.sections():
            config.add_section('Registration')

        # Set user data
        config.set('Registration', 'username', username)
        config.set('Registration', 'email', email)
        config.set('Registration', 'phone', phone)

        # Write data to file
        with open('registration.ini', 'w') as config_file:
            config.write(config_file)

        # Open main.py using subprocess.Popen when registration is successful
        subprocess.Popen(["python", "main.py"])


# Load the KV string defining the screen
kv = '''
    <RegistrationScreen>:
        name: "registration_screen"
    '''

# Load the KV string into the application
Builder.load_string(kv)


# Define the MDApp subclass
class RegistrationApp(MDApp):
    def build(self):
        # Create a screen manager to manage different screens
        self.manager = ScreenManager()

        # Add the RegistrationScreen as the first screen
        self.manager.add_widget(RegistrationScreen())

        # Return the screen manager
        return self.manager


# Run the RegistrationApp
if __name__ == "__main__":
    RegistrationApp().run()

现在这是registration.ini 文件,其中包含

[报名] 用户名 = 电子邮件= 电话=

这是他们应该在注册后重定向到的 main.py 页面代码,但是当他们单击 Registration_screen.py 中的注册按钮时,他们不会重定向到 main.py 并且 Registration.ini 文件中的代码被删除,请帮助我使这个应用程序成为可能,因为我真的感到沮丧和沮丧,这是他们应该被重定向到的 main.py 代码。

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.bottomnavigation import MDBottomNavigation
from kivy.lang import Builder

KV = '''
<MDBottomNavigation>:
   MDBottomNavigationItem:
      name: 'screen1'
      text: 'Android'
      icon: 'android'
      MDLabel:
         text: 'Android'
         halign: 'center'

   MDBottomNavigationItem:
      name: 'screen2'
      text: 'Apple'
      icon: 'apple'
      MDLabel:
         text: 'Apple'
         halign: 'center'

   MDBottomNavigationItem:
      name: 'screen3'
      text: 'Linux'
      icon: 'linux'
      MDLabel:
         text: 'Linux'
         halign: 'center'

   MDBottomNavigationItem:
      name: 'screen4'
      text: 'Windows'
      icon: 'microsoft-windows'
      MDLabel:
         text: 'Windows'
         halign: 'center'

   MDBottomNavigationItem:
      name: 'screen5'
      text: 'YourItem'
      icon: 'linux'
      MDLabel:
         text: 'YourItem'
         halign: 'center'
'''

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass

class Screen3(Screen):
    pass

class Screen4(Screen):
    pass

class Screen5(Screen):
    pass

sm = ScreenManager()
sm.add_widget(Screen1(name='screen1'))
sm.add_widget(Screen2(name='screen2'))
sm.add_widget(Screen3(name='screen3'))
sm.add_widget(Screen4(name='screen4'))
sm.add_widget(Screen5(name='screen5'))

class TutorialsPointApp(MDApp):
    def build(self):
        Builder.load_string(KV)
        return MDBottomNavigation()

if __name__ == '__main__':
    TutorialsPointApp().run()

单击它时,会删除registration.ini中的整个代码,并在单击注册按钮后重定向到main.py页面,请帮助我使该项目成为可能。

python redirect pycharm kivy storage
1个回答
0
投票

我怀疑你的代码中没有捕获到

Exception

config.write(config_file)

write()
方法在写入最后读取的文件时不带任何参数。 尝试将该行更改为:

config.write()

事实上,您不需要代码:

with open('registration.ini', 'w') as config_file:
© www.soinside.com 2019 - 2024. All rights reserved.