Update a entire window Kivy in Python

175 views
Skip to first unread message

Sylsylval Green

unread,
May 20, 2021, 3:17:23 AM5/20/21
to Kivy users support

Hello,
I'm a very new user of kivy and I would like some help on a way to update a whole window.
I think it's possible but I'm not sure.
I've seen the Clock method but I don't know if it's the only way.

Here is a part of my programme, the second window must be update according to the file chosen.

Thank you in advance for your answers.


sylsylval_test.py

Elliot Garbus

unread,
May 20, 2021, 9:21:54 AM5/20/21
to kivy-...@googlegroups.com

What is the problem you having?  I changed the dir in the listdir call, and the code seems to be working.

 

 

--
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/2190a105-ef8e-42d3-a44e-0879b15652a1n%40googlegroups.com.

 

Sylsylval Green

unread,
May 20, 2021, 10:13:38 AM5/20/21
to Kivy users support
Oh, sorry I really didn't explain myself well. The problem is that when a file button is clicked, the window called Open must take the name of the file. The first time, it works. But then, when you go back to the main window and that you ckick on an other file button, the name of the Open window is the same that the first time. I've try to remove all the widget because the build() fonction is activedd by the file button but it doesn't seem to work. 

The programme's folder is like that by the way :
 - sylsylval_test.py
 - fiche : - fiche1.txt
               - fiche2.txt


Elliot Garbus

unread,
May 21, 2021, 10:07:47 AM5/21/21
to kivy-...@googlegroups.com

There are a few things here to fix.  I’m surprised that this code works.  The class OpenWindow should be on the far left, not nested under the MainWIndow.

When the file is selected, you can use that to set a property on the other screen.

 

I would recommend using kv to setup your screens it would simplify things greatly. 

Give it a try – I’d be happy to help.

 

 

 

import os
from re import template
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.core.window import Window


class MainWindow(Screen):

   
def build(self):
       
self.name = 'Main'
       
self.Box1 = BoxLayout(orientation='horizontal', padding=10)
       
self.Box1.Gauche_Layout = GridLayout(cols=1, size_hint=(0.85, 1))
       
self.Box1.Gauche_Layout.add_widget(Label(text="Fichiers déjà existants : ", size_hint=(1, 0.1)))
       
self.Box1.Gauche_Layout.Fichier_Layout = GridLayout(cols=2)
       
self.Mes_Boutons_fichier()
       
self.Box1.Gauche_Layout.add_widget(self.Box1.Gauche_Layout.Fichier_Layout)
       
self.Box1.add_widget(self.Box1.Gauche_Layout)
       
self.add_widget(self.Box1)
        sm.add_widget(
self)

   
def Mes_Boutons_fichier(self):
       
self.Liste_Boutons_fichier = []
        i =
0
       
fichiers = os.listdir("."# finding files in folder
       
for elt in fichiers:  # creating buttons for every files
           
self.Liste_Boutons_fichier.append(Button(text=str(elt)))
           
self.Liste_Boutons_fichier[i].bind(on_press=self.p)
           
self.Box1.Gauche_Layout.Fichier_Layout.add_widget(self.Liste_Boutons_fichier[i])
            i +=
1

   
def p(self, instance):
       
print(instance.text)
        r = instance.text 
# r = name of the file chosen

       
class OpenWindow(Screen):

           
def build(self):
               
self.name = 'Open'  # Name of the window

               
self.box = BoxLayout(orientation='horizontal')
               
self.box.side = BoxLayout(orientation='vertical')
               
self.box.side.add_widget(Label(text=r))

               
self.box.add_widget(self.box.side)
               
self.box.box_dp = BoxLayout(orientation='horizontal')

               
self.box.box_dp.btn = Button()
               
self.box.box_dp.btn.bind(on_press=self.btn_f)
               
self.box.box_dp.add_widget(self.box.box_dp.btn)

               
self.box.add_widget(self.box.box_dp)
               
self.add_widget(self.box)

                sm.add_widget(
self)

           
def btn_f(self, instance):
                sm.current =
'Main'

        
Open = OpenWindow()
        Open.build()
        sm.current =
'Open'


sm = ScreenManager()


class MainFDRApp(App):

   
def build(self):
        Main = MainWindow()
        Main.build()
        sm.current =
'Main'

       
return sm


if __name__ == '__main__':
    MainFDRApp().run()

Elliot Garbus

unread,
May 21, 2021, 10:31:15 AM5/21/21
to kivy-...@googlegroups.com

Here is an alternative implementation using kv:

 

import os
from kivy.app import App
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import
Screen

kv =
"""
ScreenManager:
    id: sm
    MainWindow:
        name: 'Main'
    OpenWindow:
        name: 'Open'

<MainWindow>:
    BoxLayout:
        orientation: 'vertical'
        padding: 10
        Label:
            text: 'Fichiers déjà existants : '
            size_hint_y: None
            height: 24
        GridLayout:
            id: grid
            cols: 2

<OpenWindow>:
    BoxLayout:
        Label:
            id: filename
        Button:
            text: 'Home'
            on_release: root.manager.current = 'Main'
        
<FileButton>:
    on_release:
        app.root.current = 'Open'
        app.root.get_screen('Open').ids.filename.text = self.text
       

"""


class FileButton(Button):
   
pass


class
MainWindow(Screen):

   
def on_kv_post(self, base_widget):
       
self.add_buttons()

   
def add_buttons(self):
       
for file in os.listdir('.'):
           
self.ids.grid.add_widget(FileButton(text=file))


class OpenWindow(Screen):
   
pass


class
MainFDRApp(App):
   
def build(self):
       
return Builder.load_string(kv)


Sylsylval Green

unread,
May 21, 2021, 2:12:12 PM5/21/21
to Kivy users support

Thanks a lot ! I'll carefully read it and I'll get back in a few days (or maybe a week). For the moment, I stay without using kv but I think  I'll begin to use it a bit (so thanks for the help proposition). 

The indented class was the technic I used to replace the update of the page (it is a homemade, so it is maybe why it's wobbly).

Reply all
Reply to author
Forward
0 new messages