[英]How to read multiple text input from user
I am creating an app for calculating centroid ,orthocenter, etc. So I needed to take input from the user but I'm having trouble in reading the data entered by them.我正在创建一个用于计算质心、正交中心等的应用程序。所以我需要从用户那里获取输入,但我在读取他们输入的数据时遇到了麻烦。
I get an error, Error says : screenmanager has no attribute text
我收到一个错误,错误提示: screenmanager has no attribute text
Code:代码:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.textfield import MDTextField
from kivymd.uix.label import MDLabel
from kivymd.uix.button import MDIconButton
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.button import MDRoundFlatButton
# Builder
helper_string = """
ScreenManager:
Entry:
<Entry>:
name:"input"
MDLabel:
text:"X1"
halign:"left"
pos_hint:{'center_y': 0.95}
MDTextField:
id: x1
hint_text:"Enter X1"
helper_text: "abscissa"
helper_text_mode:"on_focus"
pos_hint:{'center_x':0.17, 'center_y':0.95}
size_hint:(0.1,0.1)
width:5
MDLabel:
text:"Y1"
halign:"center"
pos_hint:{'center_x': 0.40, 'center_y': 0.95}
MDTextField:
id: y1
hint_text:"Enter Y1"
helper_text: "ordinate"
helper_text_mode:"on_focus"
pos_hint:{'center_x':0.55, 'center_y':0.95}
size_hint:(0.1,0.1)
width:5
MDLabel:
text:"X2"
halign:"left"
pos_hint:{'center_y': 0.85}
MDTextField:
id: x2
hint_text:"Enter X2"
helper_text: "abscissa"
helper_text_mode:"on_focus"
pos_hint:{'center_x':0.17, 'center_y':0.85}
size_hint:(0.1,0.1)
width:5
MDLabel:
text:"Y2"
halign:"center"
pos_hint:{'center_x': 0.40, 'center_y': 0.85}
MDTextField:
id: y2
hint_text:"Enter Y2"
helper_text: "ordinate"
helper_text_mode:"on_focus"
pos_hint:{'center_x':0.55, 'center_y':0.85}
size_hint:(0.1,0.1)
width:5
MDLabel:
text:"X3"
halign:"left"
pos_hint:{'center_y': 0.75}
MDTextField:
id: x3
hint_text:"Enter X3"
helper_text: "abscissa"
helper_text_mode:"on_focus"
pos_hint:{'center_x':0.17, 'center_y':0.75}
size_hint:(0.1,0.1)
width:5
MDLabel:
text:"Y3"
halign:"center"
pos_hint:{'center_x': 0.40, 'center_y': 0.75}
MDTextField:
id: y3
hint_text:"Enter Y3"
helper_text: "ordinate"
helper_text_mode:"on_focus"
pos_hint:{'center_x':0.55, 'center_y':0.75}
size_hint:(0.1,0.1)
width:5
"""
class Entry(Screen):
pass
sm = ScreenManager()
sm.add_widget(Entry(name='input'))
class TriangleApp(MDApp):
def build(self):
screen = Screen()
self.theme_cls.primary_palette = "Green"
button = MDRoundFlatButton(text="Submit",
pos_hint={'center_x': 0.3, 'center_y': 0.60}, on_release=self.show)
self.help_str = Builder.load_string(helper_string)
screen.add_widget(self.help_str)
screen.add_widget(button)
return screen
def show(self, obj):
entr = sm.get_screen('input')
xx1 = entr.ids['x1'].txt
yy1 = entr.ids['y1'].txt
xx2 = entr.ids['x2'].txt
yy2 = entr.ids['y2'].txt
xx3 = entr.ids['x3'].txt
yy3 = entr.ids['y3'].txt
print(xx1, yy1, xx2, yy2, xx3, yy3)
TriangleApp().run()
I tired printing the values in console using show(self, obj) function but it didn't work.我厌倦了使用 show(self, obj) 函数在控制台中打印值,但它不起作用。
I added id to each MDTextField and tried calling them in show() but it showed a TypeError我向每个 MDTextField 添加了 id 并尝试在 show() 中调用它们,但它显示了 TypeError
I want all the 6 coordinates to be submitted at once on clicking submit button.我希望在单击提交按钮时一次提交所有 6 个坐标。 Any help would be appreciated.任何帮助,将不胜感激。
So here is a little code you can input how many inputs you want then just press Ctrl + D and it will store all the data at random.所以这里有一个小代码,你可以输入你想要的输入数量,然后只需按Ctrl + D ,它就会随机存储所有数据。 Like to be precise it will make a list name random or whatever you wanna call it.准确地说,它会使列表名称随机或您想命名的任何名称。
import sys
random = sys.stdin.readlines()
print(random)
Or you can refer to this site https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python/ it is another way to take inputs.或者你可以参考这个网站https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python/这是另一种获取输入的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.