How to Make Kivy File Chooser auto refresh in python - 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.

Related

App.root must be an _instance_ of Widget. Invalid instance in App.root

I'm not exactly new to python but wouldn't I know it well and fully, but I'm completely new to Kivy. It's just a small thing I'm doing just to start somewhere. I'm working in Visual Studio Code(VSC). Python version 3.10. The idea is: there are 5 buttons, each button has a name, when you click on a button, image(meme) should show up. Here is the full code, terminal, debugger and what is in command prompt:
code
code
Terminal
Terminal
Debugger
Debugger
Command Prompt
Command Prompt
Here is python itself and a folder "Projects"
C:\Users\Кирилл\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10
enter image description here
Here is folder with virtual environment(App_EPQ) and a folder with code(App_code)
C:\Users\Кирилл\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Projects
enter image description here
Thanks in advance
Tried to rewrite the code but that didn't work. Tried to find someone with a similar problem no luck there also. No idea what to do next and how to fix it so this is my last hope.
Your class has to inherit a layout as well as App:
class HBoxLayoutExample(BoxLayout, App)
Another way to do it would be to create a class that builds the app and returns the layout:
class HBoxLayoutExample(BoxLayout)
**your code**
class HBoxApp(App):
def build(self):
return HBoxLayoutExample
if __name__ == '__main__':
HBoxApp().run()
as shown here https://kivy.org/doc/stable/guide/lang.html#designing-with-the-kivy-language
Replace your line:
return layout, button
with:
return layout
That's all. build() method have to return one widget, which in your case is BoxLayout instance.

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

Changing screens in Python without Screen Manager Class

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.

I need a way to pull values from a text input written in .kv string to a function in my python functions

So I've created a GUI for a program I'm writing, and what I'm currently stuck on is the running of the calculations I want it to perform. I've done everything in Python 3.5, with a Kivy GUI build string to create the tabs (using tabbed panel and screen manager). I've got a button at the bottom of the tab to which this question pertains which says "Calculate" and should be taking the values of the text entries and operating them as part of the equation I want them to do.
Is the
.get
function going to be helpful in this case for doing this, or do I want to call more like:
class TabSys(TabbedPanel):
def calculate_psc_clicked(self):
#when they click the button which is id'd as "calculate_psc," this function
#will pull the values and perform the calculations
self.Cp_entry = self.Cp_entry.text
self.P_entry = self.P_entry.text
self.lhv_entry = self.lhv_entry.text
The above code is the TabSys class for my build string in kivy language, after it comes the
class nyce_meemApp(App):
def build(self):
return TabSys()
if __name__ == '__main__':
nyce_meemApp().run()
To actually run all the code. I don't want to inundate the question with code, so I'll leave it there, and I can put up any other pieces of code that anybody wants (if need be I can share the file).
Thanks

Black empty window Kivy

I made GUI for my program. When I launch app, then it appears empty black screen.
This isn't completed program, but I expect a working application.
Python code: http://pastebin.com/RbMKAd9t
Kivy interface: http://pastebin.com/12eHp0y4
I infer that your kv file is called interface.kv, and you load it only with Builder.load_string. In this case the problem is that Builder.load_string doesn't return anything because there is no root widget definition (i.e. without the <>) in the file.
Add a root widget definition, or change the build method to return Main().

Categories