Changing screens in Python without Screen Manager Class - python

I'm using gestures in all of my screens, and I cannot use a screen manager class to manage my screens, or so I believe. I can navigate the .kv file by using manger.current = 'some_screeen' but cannot in the .py file.
I've been trying Runner().ids.manager.current = 'some_screen' in the .py file but it doesn't work. There isn't even an error thrown. The screen doesn't change at all.
Essential Code (for the sake of brevity):
class Runner(gesture.GestureBox):
pass
MyApp(App):
def build(self):
return Runner()
Then in the KV file, I'm creating the screen manager.
<Runner>:
ScreenManager:
id: manager
Screen:
name: 'main_screen'
Button:
on_press:
manager.current = 'screen1'
Screen:
name: 'screen1'
Button:
on_press:
manager.current = 'home_screen'

I've been trying Runner().ids.manager.current = 'some_screen' in the .py file but it doesn't work. There isn't even an error thrown. The screen doesn't change at all.
It works fine, it just doesn't do what you believe. When you write Runner() you get a new instance of the Runner class, with its own children including its own ScreenManager. This one has nothing to do with the one you're displaying in your gui. When you set its current property the ScreenManager will dutifully change the screen, it's just you have no way to see that.
What you actually want is to change the current property of the widget that you are displaying in your gui. The best way to do this depends on the context, which you have omitted (always try to provide a full runnable example, it isn't clear what your failing code looked like). However, in this case the Runner instance is your root widget which is accessible with App.get_running_app().root, so you can write App.get_running_app().root.ids.manager.current = 'some_screen'. Again, there might be neater ways to do it depending on how you structure your code, but this is always an option.

Related

Can't change image source from outside class

I want to change the source path of an image from outside of the class/screen where the image is shown.
I have a callback function called my_callback, which will be called at some point during runtime of the app:
def my_callback():
# do stuff
MDApp.get_running_app().manager.get_screen('my_class').ids.imageID.source = "my_image.png"
MDApp.get_running_app().manager.current = "my_class"
I would expect the above two lines of code to do the following:
Switch the kivy screen to the my_class screen
Update the source of the image with the id "imageID" (as defined in my .kv file).
Outcome (1) is successful, but outcome (2) is not: rather than showing the image "my_image.png", a black shape is shown of equivalent dimensions to the image "my_image.png".
How can I fix this?
Note that MDApp is used here in place of App, as I am using the KivyMD library for my project.
I still don't know why the method I posted in the question doesn't work, but I have found an alternative solution.
The function my_callback now looks like this:
def my_callback():
# do stuff
MDApp.get_running_app().manager.current = "my_class"
image = AsyncImage(source='my_image.png')
MDApp.get_running_app().manager.get_screen('my_class').ids.floatLayoutID.add_widget(image)
And in the .kv file, there is a nested floatLayout:
<MyClass>:
name: "my_class"
FloatLayout:
FloatLayout:
id: floatLayoutID

Kivy: set widget disabled property in python code

I have something like this in kivy lang file (pseudo code)
<RootWidget>:
Checkbox:
id: chkbox
TextInput:
id: in_text
text: ""
Button:
id: ok_btn
label: "Okay"
on_press: app.ok_pressed()
disabled: chkbox.active or len(in_text.text) > 8 and ...
The point is, the ok_btn needs to be enabled and disabled dynamically based on state of several other widgets.
This all works as expected, but now I have a problem. For complicated reasons, I need to create the button and insert it into the root widget in python rather than define it in a .kv file or string. I can't figure out what to do with the disabled property. If I set it as a property
btn = Button()
btn.disabled = ...
This only sets the initial state. I thought maybe
btn.bind(on_disabled=some_function)
but this is only to do something when the button is disabled, not to define when it should be disabled. Ditto on_state. I also tried
btn.bind(disabled=some_function)
some_function is never called
Thanks in advance for any pointer
It sounds like you have it backwards: it isn't the button's disabled property that you want to bind things to, but rather you want to bind to other things so that when they change the button's disabled property gets updated.
For instance, from your original example the autogenerated code is along the lines of chkbox.bind(active=lambda self: setattr(ok_btn, "disabled", self.active) (not actually this code, but something equivalent). You need to replicate this manually.
Of course you can abstract this in various ways. For instance, you could bind all the conditions you care about to update a property of your App class (so that it's always present to update, regardless of whether your button exists), then use a kv rule like disabled: app.that_property in your button. This is not the only option though.

How to Make Kivy File Chooser auto refresh in python

So I have been coding this file explorer program. I have coded the copy function so far and the problem I am running into is that after I copy a file to its new directory(i.e. the directory that I am on currently in the filechooser) the file isn't showing up in the the actual filechooser. I am using the icon view and I saw only one mention of this online here:Refresh / Reload FileChooser and I tried this method and like the actual _update_files() function is getting executed.(checked it with a couple of print statements) But I am noticing that there is not any change in the actual filechooser.What am i doing wrong here? THANK YOU!!!
This is the python code that is executed when ever the file is copied. I assigned the actual screen that contains the file chooser to a variable named MainScreenVar
for i in range(0, self.execute_data_length):
if self.execute_data[i][2] == "File" :
shutil.copy2(self.execute_data[i][1], current_path)
MainScreenvar = MainScreen()
return MainScreenvar.ids.filechooser._update_files()
mycursor.execute("DELETE FROM selection")
mydb.commit()
This is part of the kivy file:
<MainScreen>:
FileChooserIconView:
id:filechooser
size_hint:1,.9
pos_hint:{"top": .9}
color: 0,0,0,0
and the source code for the kivy file chooser is here:
https://kivy.org/doc/stable/_modules/kivy/uix/filechooser.html
The error in your code is the line:
MainScreenvar = MainScreen()
which is creating a new instance of MainScreen. This will not be the instance that is displayed in your GUI, so the line:
return MainScreenvar.ids.filechooser._update_files()
is calling _update_files() for a FileChooserIconView that is not displayed in your GUI. The solution is to use a reference to the actual FileChooserIconView that is in your GUI. Probably something like:
MainScreenvar = self.manager.get_screen('main')
However, this is only a guess since you haven't provided much of your code. This assumes that your posted code is from a Screen, and that the name that you provided for MainScreen is main.

kivy app runs without implementing the build() method

Was wondering why the Kivy code kept on showing me the same black window despite doing some updates on the kv file. Then noticed I had a typo on the buidl() method.
From the docs "...implementing its build() method so it returns a Widget instance (the root of your widget tree)
...", you have to implement the method.
Why does this code run and give the default black window?
# game.py
from kivy.app import App
from kivy.uix.widget import Widget
class Game(Widget):
pass
class GameApp(App):
def buidl(self):
return Game()
GameApp().run()
The kv file
#game.kv
<Game>:
canvas:
Color:
rgb: .5,.5, 1.0
Rectangle:
pos: 0,0
size: self.size
Running kivy 1.11.1 python 3.7
Kivy apps have a default build() method, which you can see here; it just returns an empty widget. Generally kivy has two methods to create the root widget tree, either through overriding build() or by defining a root widget in a kv file. For more information see the documentation on creating an application.
Your quote can be found in kivy basics, before your quoted sentence:
Creating a kivy application is as simple as:
I guess the authors decided to keep the basic tutorial easy and did not mention the default implementation of build, as it doesn't really do anything useful. They also omitted the kv way of defining the root widget; again I would guess to not overwhelm the reader in this first introduction.

Using external .kv file vs doing things internally?

I have noticed that most examples I foind online don't have an external .kv file. They define all the instances internally. However they also say that having an external .kv file is a good practice. Which is better to do? If having external .kvfiles are better, then how am I supposed to use the code which uses internal code and turn it into external .kv files?
For example, doing this ->
from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class TutorialApp(App):
def build(self):
b = BoxLayout(orientation='vertical')
t = TextInput(font_size=150,
size_hint_y=None,
height=200,
text='default')
f = FloatLayout()
s = Scatter()
l = Label(text='default',
font_size=150)
t.bind(text=l.setter('text'))
f.add_widget(s)
s.add_widget(l)
b.add_widget(t)
b.add_widget(f)
return b
if __name__ == "__main__":
TutorialApp().run()
Instead of-
<ScatterTextWidget>:
orientation: 'vertical'
TextInput:
id: my_textinput
font_size: 150
size_hint_y: None
height: 200
text: 'default'
FloatLayout:
Scatter:
Label:
text: my_textinput.text
font_size: 150
"Internal" usage of kv is through Builder class, which allows you even to load external file. Those examples are worded in a Builder.load_string(...) way because it's way simpler to have a small example in one place in one file.
How to convert it to the external one? Simple, copy&paste the string from Builder.load_string() into a separate .kv file with a name of your class that inherits from App(your main class with build()) and that's it. It'll load the same thing from the external file.
Why it's better or worse? Isn't any of that actually. It's like comparing "java" and python style i.e. either putting everything out of your file and having basically this construction a'la java, where the main file contains this:
class This(something):
SpecialClass.doThis()
AnotherClass.doThat()
and other classes(or different things) are in separate files. Or this construction:
class Special(...):
...
class Another(...):
...
class This(something):
Special.do_this()
Another.do_that()
Both of them are useful and you'll find yourself working with a mix between them. It's about transparency and clearness of your code, but maybe you don't want to have a hundred of files... or a 2MB main.py, pretty much compromise of how do you decide to code.
Edit:
The python vs kv is a funny "fight", but except a for(and while?) loop you can pretty much do everything necessary inside kv in such an easy way! Kv is here to make writing easier e.g. remove too much stuff like add_widget() or basically making an empty class just to rename a widget or to change its size for using it in one place.
With python in a 500line file without kv you won't do that much as with 100 extra lines in kv. The documentation has important parts in python and maybe it's even targeted for users who can't/don't want to use kv. Also this and all examples highly depend on the author of an example and that particular part of the docs.
Which returns me back to the java vs python style of coding I mentioned before. It's pointless to do complicated stuff just because you think it'll feel/look better if you can do it cleaner and more readable i.e. don't just go one way if you have a tool that increase your speed of coding exponentially. Find the middle way.

Categories