我正在嘗試訪問另一個方法中的變數。但是,我收到一個錯誤。
我正在根據用戶傳遞的數字創建 TextInput,然后當用戶填寫該 TextInput 時,我想獲取用戶撰寫的文本并在按下按鈕后將其用于特定目的。我正在嘗試通過 App.get_running_app().root.something 來做到這一點
Python代碼:
import sys, os, pyautogui
from kivy.app import App
from kivy.resources import resource_add_path
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class MeuLayout(ScrollView):
def criar_pessoa(self, numero):
if numero.isnumeric():
if int(numero) > 20:
pyautogui.alert('Excedeu o valor limite!')
else:
i=1
nome = []
sobrenome = []
email = []
telefone = []
while i < int(numero) 1:
self.ids.grid.add_widget(Label(text= f'Nome da {i}° pessoa:', size_hint_y= None, height= 100))
self.ids.grid.add_widget(Label(text=f'Sobrenome da {i}° pessoa:', size_hint_y=None, height=100))
nome_pessoa = TextInput(write_tab= False, multiline=False, size_hint_y=None, height=30)
self.ids.grid.add_widget(nome_pessoa)
nome.append(nome_pessoa)
sobrenome_pessoa = TextInput(write_tab= False, multiline=False, size_hint_y=None, height=30)
self.ids.grid.add_widget(sobrenome_pessoa)
sobrenome.append(sobrenome_pessoa)
self.ids.grid.add_widget(Label(text=f'Email da {i}° pessoa:', size_hint_y=None, height=100))
self.ids.grid.add_widget(Label(text=f'Telefone da {i}° pessoa:', size_hint_y=None, height=100))
email_pessoa = TextInput(write_tab= False, multiline=False, size_hint_y=None, height=30)
self.ids.grid.add_widget(TextInput(write_tab= False,multiline=False, size_hint_y=None, height=30))
email.append(email_pessoa)
telefone_pessoa = TextInput(write_tab= False, multiline=False, size_hint_y=None, height=30)
self.ids.grid.add_widget(TextInput(write_tab= False,multiline=False, size_hint_y=None, height=30))
telefone.append(telefone_pessoa)
i = 1
if i - 1 == int(numero):
pass
else:
self.ids.grid.add_widget(Label(text='_'*60, size_hint_y=None, height=100))
self.ids.grid.add_widget(Label(text='_'*60, size_hint_y=None, height=100))
if i-1 == int(numero):
self.ids.grid.add_widget(Label(text='', size_hint_y=None, height=100))
botao = Button(text='Iniciar', size_hint_y= None, height= 50, on_release = self.enviar)
self.ids.grid.add_widget(botao)
else:
pyautogui.alert('Coloque apenas números!')
self.ids.numero_pessoas.text = ''
def enviar(self, widget):
n = App.get_running_app().root.criar_pessoa.nome[0].text
print(n)
class AppCadastro(App):
def build(self):
return MeuLayout()
if __name__ == '__main__':
if hasattr(sys, '_MEIPASS'):
resource_add_path(os.path.join(sys._MEIPASS))
AppCadastro().run()
基維代碼:
<MeuLayout>
scroll_wheel_distance:80
GridLayout:
cols: 2
id: grid
size_hint_y: None
height: self.minimum_height
Label:
text: 'Seu email:'
font_size: 17
size_hint_y: None
height: 100
AnchorLayout:
anchor_x: 'left'
anchor_y: 'center'
TextInput:
id: email
write_tab: False
multiline: False
size_hint_x: None
width: 300
size_hint_y: None
height: 30
Label:
text: 'Sua senha:'
font_size: 18
size_hint_y: None
height: 100
AnchorLayout:
anchor_x: 'left'
anchor_y: 'center'
TextInput:
id: senha
write_tab: False
multiline: False
password: True
size_hint_x: None
width: 300
size_hint_y: None
height: 30
Label:
text : 'Quantidade de pessoas: (limite: 20)'
font_size: 18
size_hint_y: None
height: 100
AnchorLayout:
anchor_x: 'left'
TextInput:
id: numero_pessoas
write_tab: False
multiline: False
size_hint: .1, .1
size_hint: None, None
height: 30
width: 30
on_text_validate: root.criar_pessoa(numero_pessoas.text)
Label:
size_hint_y: None
height: 100
Label:
size_hint_y: None
height: 100
錯誤:AttributeError:'function'物件沒有屬性'nome'
uj5u.com熱心網友回復:
您無法訪問方法nome
中的變數,criar_pessoa()
因為它是區域變數(該方法的區域變數),并且在方法完成后立即被丟棄。如果要訪問nome
,則需要將其更改為一種在方法完成后將被維護的變數。一種方法是使其成為MeuLayout
類的實體變數。nome
只需將變數的每次出現都更改為self.nome
,它們替換:
def enviar(self, widget):
n = App.get_running_app().root.criar_pessoa.nome[0].text
print(n)
和:
def enviar(self, widget):
n = App.get_running_app().root.nome[0].text
print(n)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/495294.html