简体   繁体   中英

Trying to convert app from Tkinter to Kivy: can't get labels to update

I have a fully functioning app in Tkinter, I am trying to wrap my head around how Kivy works and have been up against a wall for a few days. I got everything laid out (finally) and completely abandoned using the .kv file. I think that if I can get a few things working through Python code then I'll start to understand the relationships between Python and Kivy. Anyways, the problem now is that I can't get labels to update when I call a function. I just want to (for now) get a label to change it's text when I press a button. Very easy in tkinter, not so here. I've tried many different combos of things to get it to work but am failing. You'll notice that the button calls the tick() function, it originally switched screens, but I changed it so I could just get it to change the label. Eventually I'd like the clock to run on the top of the screen, but will settle for just getting it to update to some arbitrary text. A reply with full code would be super!
(ps. I realize that most of you could probably have the entire app written in 30 minutes and this is very basic)

Thanks

Here is my code:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
from kivy.event import EventDispatcher

import time
import datetime

class ScreenOne(Screen):

    def __init__ (self,**kwargs):
        super (ScreenOne, self).__init__(**kwargs)

        main_box = BoxLayout(orientation='vertical')
        header_box = BoxLayout(size_hint_y=None, height=35)
        top_box = BoxLayout(size_hint_y=None, height=60)
        middle_box = BoxLayout(size_hint_y=None, height=300)
        bottom_box = BoxLayout()
        middle1_box =BoxLayout(size_hint_x=None, width=50)
        middle2_box = BoxLayout(orientation='vertical')
        list_box1 = BoxLayout()
        list_box2 = BoxLayout()
        list_box3 = BoxLayout()
        list_box4 = BoxLayout()
        list_box5 = BoxLayout()
        list_box6 = BoxLayout()

        checkmark = 'Check_Mark.jpg'
        redX = Image(source='red_X')

        my_label1 = Label(text="BlaBlaBla on screen 1", font_size='24dp')
        my_button1 = Button(text="Go to screen 2",size_hint_y=None, size_y=100)
        my_button1.bind(on_press=self.tick)
        self.date_label = Label(text="not working")
        top_label = Label(text="The top text")
        mid_label = Label(text="The mid text")
        bot_label = Label(text="The bottom text")
        header_label = Label(text=" ")
        my_image = Image(source='colours.png', allow_stretch=True, keep_ratio=False)
        my_image2 = Image(source='colours2.png', allow_stretch=True, keep_ratio=False)

        my_image3 = Image(source='red_X.gif')

        fish_icon = Image(source=checkmark)
        coral_icon = Image(source=checkmark)
        topoff_icon = Image(source=checkmark)
        water_icon = Image(source=checkmark)
        filter_icon = Image(source=checkmark)
        bulb_icon = Image(source=checkmark)

        fish_label = Label(text="Last Fish Feeding", font_size=30)
        coral_label = Label(text="Last Coral Feeding", font_size=30)
        topoff_label = Label(text="Last water Top-Off", font_size=30)
        water_label = Label(text="Last Water Change", font_size=30)
        filter_label = Label(text="Last Filter Replacement", font_size=30)
        bulb_label = Label(text="Last Bulb Replacement", font_size=30)

        last_fish_date = Label(text="This is a date", font_size=30)
        last_coral_date = Label(text="This is a date", font_size=30)
        last_topoff_date = Label(text="This is a date", font_size=30)
        last_water_date = Label(text="This is a date", font_size=30)
        last_filter_date = Label(text="This is a date", font_size=30)
        last_bulb_date = Label(text="This is a date", font_size=30)

      #  main_box.add_widget(bot_label)
        main_box.add_widget(header_box)
        main_box.add_widget(top_box)
        main_box.add_widget(middle_box)
        main_box.add_widget(bottom_box)
        header_box.add_widget(header_label)
        header_box.add_widget(self.date_label)

        top_box.add_widget(my_image)
        middle_box.add_widget(middle1_box)
        middle1_box.add_widget(my_image3)
        middle_box.add_widget(middle2_box)
        middle2_box.add_widget(list_box1)
        middle2_box.add_widget(list_box2)
        middle2_box.add_widget(list_box3)
        middle2_box.add_widget(list_box4)
        middle2_box.add_widget(list_box5)
        middle2_box.add_widget(list_box6)

        list_box1.add_widget(fish_icon)
        list_box1.add_widget(fish_label)
        list_box1.add_widget(last_fish_date)
        list_box2.add_widget(coral_icon)
        list_box2.add_widget(coral_label)
        list_box2.add_widget(last_coral_date)
        list_box3.add_widget(topoff_icon)
        list_box3.add_widget(topoff_label)
        list_box3.add_widget(last_topoff_date)
        list_box4.add_widget(water_icon)
        list_box4.add_widget(water_label)
        list_box4.add_widget(last_water_date)
        list_box5.add_widget(filter_icon)
        list_box5.add_widget(filter_label)
        list_box5.add_widget(last_filter_date)
        list_box6.add_widget(bulb_icon)
        list_box6.add_widget(bulb_label)
        list_box6.add_widget(last_bulb_date)

        bottom_box.add_widget(my_button1)
        self.add_widget(main_box)
    def tick(self, *args):
        print ("test output")
        time1 = " "
        # get the current local time from the PC
        time2 = time.strftime('%H:%M:%S')
        time3 = time.strftime('%M:%S')

        date = datetime.datetime.strftime(date, "%m-%d-%Y   ")
        if time2 != time1:
            time1 = time2
            self.time1 = time1
            self.date_label(text=date + time2)




    def changer(self,*args):
        self.manager.current = 'screen2'

class ScreenTwo(Screen):

    def __init__(self,**kwargs):
        super (ScreenTwo,self).__init__(**kwargs)

        my_box1 = BoxLayout(orientation='vertical')
        my_label1 = Label(text="BlaBlaBla on screen 2",font_size='24dp')
        my_button1 = Button(text="Go to screen 1",size_hint_y=None, size_y=100)
        my_button1.bind(on_press=self.changer)
        my_box1.add_widget(my_label1)
        my_box1.add_widget(my_button1)
        self.add_widget(my_box1)

    def changer(self,*args):
        self.manager.current = 'screen1'

class TestApp(App):

        def build(self):
            my_screenmanager = ScreenManager()
            screen1 = ScreenOne(name='screen1')
            screen2 = ScreenTwo(name='screen2')
            my_screenmanager.add_widget(screen1)
            my_screenmanager.add_widget(screen2)
            return my_screenmanager

if __name__ == '__main__':
    TestApp().run()
date = datetime.datetime.strftime(date, "%m-%d-%Y   ")

This line causes a crash because date is not yet defined, but you use it in its own definition.

self.date_label(text=date + time2)

self.date_label is a Label instance, but it's not callable. The correct way to set a kivy property is to access it as an attribute:

self.date_label.text = time2

(I removed the date part because of the above problem in its definition).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM