Only "else statement" shown when running app with python/kivy - python

I have been working on an app with Kivy that receives 0, 1 or 2 as text input by the user and a label on the final screen shows a text which should differ based on the received input. It does not present with any errors any longer, but the final screen will only show the "else" text ("Bad Luck") even when the requirements of the "if statement" are met.
By searching similar questions, I tried using an "input_filter" for integers in case it resolved the issue, but it didn't.
I am a beginner with no programming background, so please feel free to be as elaborate as you like in your answers. Thank you in advance for your time and any ideas/recommendations you may share. Following are the codes of the .py and .kv files:
.py file:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty, StringProperty, NumericProperty
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class FourthWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class MyApp(App):
def build(self):
kv = Builder.load_file("my.kv")
return kv
if __name__=="__main__":
MyApp().run()
.kv file:
WindowManager:
MainWindow:
SecondWindow:
ThirdWindow:
FourthWindow:
<MainWindow>:
name: "main"
GridLayout:
cols:1
rows:2
Label:
text: "stuff"
Button:
text: "stuff"
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
GridLayout:
cols:1
rows:2
GridLayout:
cols:2
Label:
text: "stuff"
TextInput:
id: ti_a
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_b
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_c
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_d
multiline:False
Label:
text: "stuff"
TextInput:
id: ti_e
multiline:False
GridLayout:
cols:2
Button:
text: "stuff"
on_release:
app.root.current = "third"
root.manager.transition.direction = "left"
Button:
text: "Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
<ThirdWindow>:
name: "third"
GridLayout:
cols:1
rows:2
GridLayout:
cols:2
Label:
text: "stuff"
TextInput:
id: ti_f
multiline:False
GridLayout:
cols:2
Button:
text: "stuff"
on_release:
app.root.current = "fourth"
root.manager.transition.direction = "left"
Button:
text: "Back"
on_release:
app.root.current = "second"
root.manager.transition.direction = "right"
<FourthWindow>:
name: "fourth"
Label:
text: "Stuff" if root.manager.get_screen("second").ids.ti_a.text == "0" and root.manager.get_screen("second").ids.ti_b.text == "0" and root.manager.get_screen("second").ids.ti_c.text == "0" and root.manager.get_screen("second").ids.ti_d.text == "0" and root.manager.get_screen("second").ids.ti_e.text == "1" and root.manager.get_screen("third").ids.ti_f.text == "0" else "Bad Luck"

The problem is that kivy doesn't recognize the expression:
root.manager.get_screen("second").ids.ti_a.text
as a property that it can bind to, so the value of Label text in your FourthWindow is only evaluated once (when the FourthWindow is created). Any changes to the other Labels in your app will have no effect on FourthWindow. See the documentation.
The fix is to write your own code to update that Label by adding an id to that Label:
<FourthWindow>:
name: "fourth"
Label:
id: lab
text: ""
And adding a on_pre_enter() method to FourthWindow:
class FourthWindow(Screen):
def on_pre_enter(self, *args):
if self.manager.get_screen("second").ids.ti_a.text == "0" and \
self.manager.get_screen("second").ids.ti_b.text == "0" and \
self.manager.get_screen("second").ids.ti_c.text == "0" and \
self.manager.get_screen("second").ids.ti_d.text == "0" and \
self.manager.get_screen("second").ids.ti_e.text == "1" and \
self.manager.get_screen("third").ids.ti_f.text == "0":
self.ids.lab.text = 'Stuff'
else:
self.ids.lab.text = 'Bad Luck'
This will evaluate your expression and set the text each time just before the FourthWindow is displayed.

Related

Using a Spinner for changing several Screens in kivy

I need help. I'm making an app, And when i want to use an Spinner. I assigned several values ("Catalogue","Buy","Payment Methods", "Contact"). I want that when choosing the value "Buy", My screen change to the "SecondWindow(Screen)". I was trying to do it from my py.file but I'm not sure how to do it correctly. I was trying to do it from my "def spinner_clicked(self,value):"
If you I can do it from my kv.file straightly. I would like to learn how to do it too.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.spelling import Spelling
from kivy.uix.screenmanager import ScreenManager, Screen
kv= Builder.load_file('test2.kv')
class MyLayout(Widget):
def spinner_clicked(self,value):
sm= ScreenManager()
screen= Screen(name='second')
self.ids.Label1.text= f'You selected: {value}'
#IN THIS PART WHEN PRESSING IN MY SPINNER the choice "buy" I need to change to the SecondWindow(Screen)
if self.ids.spinner_id.values == "Buy":
#self.sm.current= 'second'
#sm.root.manager.current= "second"
#sm.switch_to(screen)
pass
#Definine our different screens
class FirstWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class AwesomeApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()
kv.file
<MyLayout>
BoxLayout:
orientation:"vertical"
size: root.width, root.height
GridLayout:
cols:3
rows:3
Spinner:
id: spinner_id
text: "Menu"
values: ["Catalogue","Buy","Payment Methods", "Contact"]
on_text: root.spinner_clicked(spinner_id.text)
#on_text: root.manager.current= "second" - it doesnt' work
#I want to change the screens from here choosing the value "Buy" to the 2ndScreen
Label:
id: Label1
text: "My Panel"
Button:
text:"Buy"
WindowManager:
FirstWindow:
SecondWindow:
<FirstWindow>:
name: "first"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "First Screen"
font_size: 32
Button:
text: "Next Screen"
font_size: 32
on_release:
root.manager.current= "second"
root.manager.transition.direction= "left" #up
<SecondWindow>:
name: "second"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Second Screen"
font_size: 32
Button:
text: "Go Back"
font_size: 32
on_release:
#app.root.current= "first"
root.manager.current= "first"
root.manager.transition.direction= "right"
If you set the Screen names to the values in your Spinner, then you can modify your kv slightly. In the Spinner section:
Spinner:
id: spinner_id
text: "Menu"
values: ["Catalogue","Buy","Payment Methods", "Contact"]
on_text: root.spinner_clicked(spinner_id.text)
on_text: window_manager.current = self.text # uses an id defined in the WindowManager section
And the window_manager id is defined later in the kv:
WindowManager:
id: window_manager
FirstWindow:
SecondWindow:
The window_manager.current refers to the id defined for the WindowManager, and accesses its current property.
Note that the root.manager in the kv file only works inside a rule for a Screen, like inside <FirstWindow>: or <SecondWindow>:. In that context, the root refers to the root widget of the rule (one of those Screens), and manager then refers to the ScreenManager that contains that Screen.

Windwomanager with TappedPanels

main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.tabbedpanel import TabbedPanel
class Test(TabbedPanel):
pass
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("settings.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
settings.kv:
WindowManager:
MainWindow:
SecondWindow:
Test:
<MainWindow>:
name: "main"
GridLayout:
cols:2
Button:
text: "New Order"
on_release:
app.root.current = "tabs"
root.manager.transition.direction = "left"
Button:
text: "Basket"
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
<Test>:
name: "test"
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'first tab'
Label:
text: 'First tab content area'
TabbedPanelItem:
text: 'tab2'
BoxLayout:
Label:
text: 'Second tab content area'
Button:
text: 'Button that does nothing'
TabbedPanelItem:
text: 'tab3'
RstDocument:
text:
'\\n'.join(("Hello world", "-----------",
"You are in the third tab."))
I want a transition that starts with a button press that leads to a tabbed overlay, However the screenmanager doesnt accept the use of tabbedpanel
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
How can I achieve this effect, I am new to kivy. I want one button press to go to a another normal screen while the other one to open a tabbed screen,
Just add a TabblePanel to one of your Screens.
<BlaBlaScreen>:
name: 'BlaBla'
TabbedPanel:
do_default_tab: False
TabbedPanelItem:
text: "something"
Just put your TabbedPanel in a Screen.
Good day. Use your screen manager to switch between each screen. The screen then contains your unique layout of whatever you placed in it. That could include a screen with tabs.

Kivy Python: buttons and classes for multiple screens

I have a problem with my Kivy Python Code. I have 2 screens: 1st is to navigate to the second screen and on the 2nd screen there is a button to add text to a scrollview...navigating is working but it does not add any text to the scrollview...I think I need some help here! AttributeError: 'super' object has no attribute 'getattr'
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock, mainthread
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.lang import Builder
Builder.load_string("""
<MenuScreen>:
name: 'mainmenu'
BoxLayout:
spacing: 1
orientation: "vertical"
Label:
text: "MAIN MENU"
Button:
text: 'Go to Screen 2'
on_release:
root.manager.current = 'screen2'
root.manager.transition.direction = "left"
Button:
text: 'Quit'
on_release: root.manager.current = app.exit_software()
<Screen2>:
name: 'screen2'
BoxLayout:
spacing: 1
orientation: "vertical"
ScrollView:
id: scroll_view
always_overscroll: False
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Label:
id: label
text: "You can add some Text here by pressing the button"
size_hint: None, None
size: self.texture_size
Button:
text: 'Add text!'
size_hint_y: 0.1
on_release: app.add_text()
Button:
text: 'Back to main menu'
size_hint_y: 0.1
on_release:
root.manager.current = 'mainmenu'
root.manager.transition.direction = "right"
""")
# Declare both screens
class MenuScreen(Screen):
pass
class Screen2(Screen):
pass
class AddTextApp(App):
def __init__(self,**kwargs):
super().__init__(**kwargs)
def build(self):
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='mainmenu'))
sm.add_widget(Screen2(name='screen2'))
return sm
def add_text(self):
self.root.ids.label.text += f"Some new Text\n"
self.root.ids.scroll_view.scroll_y = 0
def exit_software(self):
App.get_running_app().stop()
if __name__ == '__main__':
AddTextApp().run()
Thank you very much in advance!
The error occurred because self.root.ids gets access to widgets located in the root widget of the main class. To access the secondary screen elements, you need to add it to the main class (in your case, in ScreenManager) and set its id. Also, you have a lot of imported excess, so that it is clearly visible, I advise you to use Pycharm or something like that.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
kv = """
<MenuScreen>:
name: 'mainmenu'
BoxLayout:
spacing: 1
orientation: "vertical"
Label:
text: "MAIN MENU"
Button:
text: 'Go to Screen 2'
on_release:
root.manager.current = 'screen2'
root.manager.transition.direction = "left"
Button:
text: 'Quit'
on_release: root.manager.current = app.exit_software()
<Screen2>:
name: 'screen2'
BoxLayout:
spacing: 1
orientation: "vertical"
ScrollView:
id: scroll_view
always_overscroll: False
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Label:
id: label
text: "You can add some Text here by pressing the button"
size_hint: None, None
size: self.texture_size
Button:
text: 'Add text!'
size_hint_y: 0.1
on_release: app.add_text()
Button:
text: 'Back to main menu'
size_hint_y: 0.1
on_release:
root.manager.current = 'mainmenu'
root.manager.transition.direction = "right"
ScreenManager:
MenuScreen:
id: menu_scr
Screen2:
id: scr_2
"""
class MenuScreen(Screen):
pass
class Screen2(Screen):
pass
class AddTextApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self):
return Builder.load_string(kv)
def add_text(self):
self.root.ids.scr_2.ids.label.text += f"Some new Text\n"
self.root.ids.scr_2.ids.scroll_view.scroll_y = 0
#staticmethod
def exit_software():
App.get_running_app().stop()
if __name__ == '__main__':
AddTextApp().run()

Changing Transitions in ScreenManager inside Kivy (.kv) file

I have been learning Kivy with a YouTube channel. I made two screens and one screenmanager but they are classes in python code. I saw examples like WinManager = ScreenManager(transition=CardTransition()) but I didn't structure my code like that. How can i change transition with my code? I tried to do it on init function of class, didn't work; and tried to add transition properity in kv file. Didn't work either.
Python:
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, CardTransition
class MainWindow(Screen):
pass
class AdminPanel(Screen):
pass
class WinManager(ScreenManager):
pass
kvTemplate = Builder.load_file("template.kv")
class MyMainApp(App):
def build(self):
return kvTemplate
if __name__ == "__main__":
MyMainApp().run()
Kivy (.kv):
WinManager:
MainWindow:
AdminPanel:
<MainWindow>:
name: "loginPanel"
nickname: nicknameID
password: passwordID
key: keyID
GridLayout:
rows: 2
GridLayout:
cols: 2
Label:
text: "Nickname: "
TextInput:
id: nicknameID
multiline: False
Label:
text: "Password: "
TextInput:
id: passwordID
multiline: False
Label:
text: "Key: "
TextInput:
id: keyID
multiline: False
Button:
text: "Log in"
on_release:
app.root.current = "adminPanel"
root.manager.transition.direction = "up"
<adminPanel>:
name: "adminPanel"
Button:
text: "Back"
on_release:
app.root.current = "loginPanel"
root.manager.transition.direction = "down"
In your kv file you can do:
#:import CardTransition kivy.uix.screenmanager.CardTransition
WinManager:
transition: CardTransition()
MainWindow:
AdminPanel:

How to update button text after pressing button in python?

I'm trying to make an app that will eventually be able to score cards. One problem I'm trying to solve is how to update the text of a button after pressing or updating it somehow. I'm newer to python/kivy. I've found a couple similar questions, dealing with functions and such, but none dealt with .kv files and separate windows. My code is below.
main.py
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MainWindow(Screen):
pass
class ScoreWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("mainkv.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
mainkv.kv
WindowManager:
MainWindow:
ScoreWindow:
<MainWindow>:
name: "main"
GridLayout:
cols: 1
Button:
text: "Quick Score"
on_release:
app.root.current = "score"
root.manager.transition.direction = "left"
<ScoreWindow>:
name: "score"
GridLayout:
cols: 7
Button:
name: "three"
text: "Press to Update"
on_release:
three.text = "Updated"
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: "Score"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: ""
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
Your Buttons can refer to themselves in kvlang using the self keyword. So just add another line that says self.text = 'foo' in the on_release: area of your Buttons.

Categories

Resources