gpt4 book ai didi

python - 从另一个类中的 InputText 获取值。基维

转载 作者:行者123 更新时间:2023-12-04 15:18:31 25 4
gpt4 key购买 nike

我正在尝试从 InputText 传递一个值,如下所示。但我总是得到一个空字符串。如果我写 InputText (text = 'test'),那么总是传输单词 test 而不是我输入的内容。如何在 UserAcount 类中获取我输入到 InputText()

中的值
from kivy.app import App 
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.metrics import dp

import sqlite3

conn = sqlite3.connect('diplom.sqlite', check_same_thread = False)


class EntryMenu(Screen):

def __init__(self, **kw):
super(EntryMenu, self).__init__(**kw)
box = BoxLayout(padding=150,orientation='vertical')
self.login = TextInput(text='', multiline=False)
box.add_widget(self.login)
self.password = TextInput(text='', multiline=False)
box.add_widget(self.password)
self.error = Label(text="")
box.add_widget(self.error)
box.add_widget(Button(text='Entry', on_press=self.buttonClicked))
self.add_widget(box)

def buttonClicked(self,btn):
self.error.text=''
with conn:
cursor = conn.cursor()
sql =f'''SELECT username,password
FROM user
WHERE username = '{self.login.text}';
'''
cursor.execute(sql)
results = cursor.fetchall()
if results:
if self.password.text==results[0][1]:
set_screen('user_acount')
elif self.password.text!=results[0][1]:
self.error.text = "error password"
else:
self.error.text = "can not search user"



class UserAcount(Screen):
def __init__(self,login, **kw):
super(UserAcount, self).__init__(**kw)
box = BoxLayout(orientation='vertical')
self.login=login
name = Label(text=self.login)
box.add_widget(name)
back_button = Button(text='Exit', on_press=lambda x:
set_screen('menu'), size_hint_y=None, height=dp(40))
box.add_widget(back_button)
self.add_widget(box)


def set_screen(name_screen):
sm.current = name_screen


sm = ScreenManager()
sm.add_widget(EntryMenu(name='menu'))
sm.add_widget(UserAcount(EntryMenu().login.text,name='user_acount'))

class MyApp(App):
def __init__(self, **kvargs):
super(MyApp, self).__init__(**kvargs)

def build(self):
return sm


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

请告诉我我缺少什么。提前致谢

最佳答案

好的。我想通了,你只在开始时传递用户名,因此以后不会改变。此代码应该有效:

from kivy.app import App 
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.metrics import dp

import sqlite3

conn = sqlite3.connect('diplom.sqlite', check_same_thread = False)


class EntryMenu(Screen):

def __init__(self,myapp, **kw):
super(EntryMenu, self).__init__(**kw)
box = BoxLayout(padding=150,orientation='vertical')
self.myapp = myapp
self.login = TextInput(text='', multiline=False)
box.add_widget(self.login)
self.password = TextInput(text='', multiline=False)
box.add_widget(self.password)
self.error = Label(text="")
box.add_widget(self.error)
box.add_widget(Button(text='Entry', on_press=self.buttonClicked))
self.add_widget(box)

def buttonClicked(self,btn):
self.error.text=''
with conn:
cursor = conn.cursor()
sql =f'''SELECT username,password
FROM user
WHERE username = '{self.login.text}';
'''
cursor.execute(sql)
results = cursor.fetchall()
if results:
if self.password.text==results[0][1]:
self.myapp.set_screen('user_acount', self.login.text)
elif self.password.text!=results[0][1]:
self.error.text = "error password"
else:
self.error.text = "can not search user"



class UserAcount(Screen):
def __init__(self,login,myapp, **kw):
super(UserAcount, self).__init__(**kw)
box = BoxLayout(orientation='vertical')
self.myapp = myapp
self.login=login
name = Label(text=self.login)
box.add_widget(name)
back_button = Button(text='Exit', on_press=lambda x:
myapp.set_screen('menu'), size_hint_y=None, height=dp(40))
box.add_widget(back_button)
self.add_widget(box)








class MyApp(App):
def __init__(self, **kvargs):
super(MyApp, self).__init__(**kvargs)
self.sm = ScreenManager()
self.username = ""
self.userAcount = UserAcount(self.username,name='user_acount', myapp=self)
self.sm.add_widget(self.userAcount)
self.entryMenu = EntryMenu(name='menu', myapp=self)
self.sm.add_widget(self.entryMenu)
self.set_screen("menu")

def build(self):
return self.sm

def set_screen(self, name_screen, username=None):
if username != None and name_screen == "user_acount":
self.username = username
self.sm.remove_widget(self.userAcount)
self.userAcount = UserAcount(self.username,name='user_acount', myapp=self)
self.userAcount.login = self.username
self.sm.add_widget(self.userAcount)

self.sm.current = name_screen


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

关于python - 从另一个类中的 InputText 获取值。基维,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63858219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com