I'm beginner in python. I started to learn kivy (maybe kivymd) 4days ago. I learned something new everyday. But I can't find any anwser on my problem. which I'm gonna share with you.
Before learning kivy, I learned tkinter. And It's my current Fav and Easy GUI module.
In Tkinter:
from tkinter import *
def show_data():
userdata=user_input.get()
print('Your Data is: ' userdata)
root=Tk()
root.title('Stackoverflow Help')
root.geometry('400x400')
user_input_text=Label(root,text='Enter Some Data',font='arial 20')
user_input_text.pack(pady=40)
user_input=Entry(root,font='20')
user_input.pack(pady=10)
user_input_button=Button(root,text='Done',font='20',command=show_data)
user_input_button.pack(pady=10)
root.mainloop()
I can Print/Get User inputed data in a Def/Function with idname.get()
Method.
But In Kivy:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
Window.size=(400,600)
please_help_me="""
MDScreen:
MDLabel:
text: 'Please Enter Some Data'
halign:'center'
pos_hint:{'center_x':0.5,'center_y':0.55}
MDTextField:
id:user_input
hint_text: 'Type Here' #How Can I Print/Get User Data outside of builder?
pos_hint:{'center_x':0.5,'center_y':0.44}
size_hint_x:None
width:300
MDRaisedButton:
text:'Done'
pos_hint: {'center_x':0.5,'center_y':0.35}
"""
class HelpPleaseApp(MDApp):
def build(self):
main=Builder.load_string(please_help_me)
return main
# I wanna create a Function Here That Print User's Inputed data from builder's 'MDTextField'
HelpPleaseApp().run()
I want to Print the MDTextField
Inputted Data while button pressed.I know on_release: print(user_input.text)
can do it, but It is inside the builder string. I want to do in in a function under main class. I also created id
for it. But don't know how to get the MDTextField.text
/User inputted data in a function and print it out. I know you will tell me to google it, But I already searched about it on google and youtube. So, I Need Your kind help(pls)
CodePudding user response:
You can add on_release: app.show_user_input(user_input.text)
under MDRaisedButton
section, then define show_user_input()
inside HelpPleaseApp
:
please_help_me="""
MDScreen:
MDLabel:
text: 'Please Enter Some Data'
halign:'center'
pos_hint:{'center_x':0.5,'center_y':0.55}
MDTextField:
id:user_input
hint_text: 'Type Here' #How Can I Print/Get User Data outside of builder?
pos_hint:{'center_x':0.5,'center_y':0.44}
size_hint_x:None
width:300
MDRaisedButton:
text:'Done'
pos_hint: {'center_x':0.5,'center_y':0.35}
on_release: app.show_user_input(user_input.text)
"""
class HelpPleaseApp(MDApp):
def show_user_input(self, text):
print(text)
def build(self):
main = Builder.load_string(please_help_me)
return main