Okay so I am very new to python and kivy. I was learning to make a simple GUI so I can upgrade it later once I learn more. Everything is good so far except I want to pass
the value of tempData from the function FitToInches to be displayed as a Label in <ResultWindow>. I have tried going back and forth with many techniques and this is the
closest where I got before being stuck. Can anyone help me how to do it without using too many new things other than this, since I couldn't understand some of the answers
I found on the google. For what it's worth, I can print the value of tempData, so basically I have successfully accessed UserSource from <AskUserWindow>. I created a
PassResult Function but I don't know if I even did it right. I'd appreciate any help. Thank you.
convertunit.kv
scrManager:
AskUserWindow:
ResultWindow:
<AskUserWindow>:
name: "askUser"
userSource: userSource
GridLayout:
cols: 2
Label:
text: "Input the number: "
TextInput:
id: userSource
multiline: False
Button:
id: toFootBtn
text: "Convert Inches to Foot"
on_press:
app.root.InchToFeet(userSource)
on_release:
app.root.current = "result"
root.manager.transition.direction = "left"
Button:
id: toInchBtn
text: "Convert Foot to Inches"
on_press:
app.root.FeetToInches(userSource)
on_release:
app.root.current = "result"
root.manager.transition.direction = "left"
<ResultWindow>:
name: "result"
GridLayout:
cols: 1
Label:
text: "Your result is :" + app.resultData2
Button:
text: "Convert More numbers"
on_release:
app.root.current = "askUser"
root.manager.transition.direction = "right"
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
class AskUserWindow(Screen):
pass
class ResultWindow(Screen):
pass
class scrManager(ScreenManager):
def PassResult(self, stringInput):
MyMainApp.resultData2 = stringInput
def FeetToInches(self, userSource):
feetData = userSource
tempInput = userSource
tempData = str(12 * int(tempInput.text))
self.PassResult(tempData)
def InchToFeet(self, userSource):
inchData = userSource.text
#tempData2 = str(int(inchData) / 12)
kv = Builder.load_file("convertunit.kv")
class MyMainApp(App):
def build(self):
resultData2 = "buildAlter"
return kv
if __name__ == "__main__":
MyMainApp().run()
I tried to make a minimum number of changes. Happy to answer any questions.
The KV code:
ScrManager:
id: sm
AskUserWindow:
name: "askUser" # name goes with instance not definition
ResultWindow:
name: "result"
<AskUserWindow>:
# userSource: userSource
GridLayout:
cols: 2
Label:
text: "Input the number: "
TextInput:
id: userSource
multiline: False
Button:
id: toFootBtn
text: "Convert Inches to Foot"
on_press:
app.root.InchToFeet(userSource)
on_release:
app.root.current = "result"
root.manager.transition.direction = "left"
Button:
id: toInchBtn
text: "Convert Foot to Inches"
on_press:
app.root.FeetToInches(userSource)
on_release:
app.root.current = "result"
root.manager.transition.direction = "left"
<ResultWindow>:
on_enter:
answer.text = 'Your Result: ' + app.result # need an event to reevaluate the string,
GridLayout:
cols: 1
Label:
id:answer
Button:
text: "Convert More numbers"
on_release:
app.root.current = "askUser"
root.manager.transition.direction = "right"
The Python Code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
class AskUserWindow(Screen):
pass
class ResultWindow(Screen):
pass
class ScrManager(ScreenManager):
# def PassResult(self):
# app = App.get_running_app() # get the pointer to app using get_running_app()
# app.resultData2 = stringInput
def FeetToInches(self, usersource):
# feetData = userSource
# tempInput = userSource
# tempData = str(12 * int(tempInput.text))
#self.PassResult(tempData)
app = App.get_running_app()
app.result = str(12 * int(usersource.text))
def InchToFeet(self, user_source):
#inchData = userSource.text
#tempData2 = str(int(inchData) / 12)
app = App.get_running_app()
app.result = f'{int(user_source.text)/12:0.2f}'
kv = Builder.load_file("convertunit.kv")
class
MyMainApp(App):
result = StringProperty("buildAlter") # Create a kivy string property
def build(self):
return kv
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e3a07436-d05a-45c4-bf08-458c7d49b91f%40googlegroups.com.
For #2: Start with: https://kivy.org/doc/stable/guide/packaging-android.html
I have not done any android packaging, but many others have. Follow the directions, if you have problems others will help.
An id is a pointer to a widget. In kv you can use the id to access all of the attributes of a widget. In python the id is stored in a dict called ids.
from kivy.app import App
from kivy.lang import
Builder
kv = """
<CustomButton@Button>:
font_size: sp(50)
BoxLayout:
orientation: 'vertical'
Label:
id: label_1
font_size: sp(50)
text: 'Button Test'
CustomButton:
id: b1 # ids is created from the id in kv
text: 'Print all ids'
on_release:
print(f'The ids: {app.root.ids}') # print all the ids, you can also use ids from python
label_1.text = 'Button 1 Pressed' # using an id to write to an attribute
CustomButton:
id: b2
text: 'Button 2'
on_release: label_1.text = 'Button 2 Pressed' # using the id label_1 to write to the text in the label
CustomButton:
id: b3
text: 'Button 3'
on_release:
label_1.text = 'Button 3 Pressed'
b1.text = 'Text Changed by Button 3'
"""
class BoxesApp(App):
def build(self):
return Builder.load_string(kv)
BoxesApp().run()
From: 'Prabhat K.C.' via Kivy users support
Sent: Sunday, February 16, 2020 3:09 PM
To: Kivy users support
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/95129300-3ec0-4249-b8b4-edca637a2722%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
2. What is the best way to package pycharm + kivy file as an android app? Since that is where I am trying to head to.
2. What is the best way to package pycharm + kivy file as an android app? Since that is where I am trying to head to.
That's exactly what I want. Now in my code, if I want to use a same button (I am thinking of a home button) at various screens, where do I put the original custom button@button such that I won't be thrown a KV file only allow one root object. Thank you.
Kivy has a global name space. So you can put you home button in any kivy file – even if you have multiple kivy files and use it anywhere.
You are creating a ‘rule’ when you use the <MyButton@Button>, this is analogous to creating a class in python. This is the not the root widget of the app.
Alternately you can create:
In Kv:
<MyButton> :
And in Python:
Class MyButton(Button):
# if you want to define additional methods in your custom button.
def my_method(self):
# your custom code here….
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/411ddc3d-2c4e-4350-b45b-8c8f211e3e8e%40googlegroups.com.