繁体   English   中英

尝试将应用程序从Tkinter转换为Kivy:无法更新标签

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

我在Tkinter中有一个功能齐全的应用程序,我试图围绕Kivy的工作原理,并在墙上碰了几天。 我把所有东西都布置好了(最终),并使用.kv文件完全放弃了。 我认为,如果我能通过Python代码完成一些工作,那么我将开始理解Python和Kivy之间的关系。 无论如何,现在的问题是调用函数时无法获取标签进行更新。 我只想(暂时)按一下按钮时获得一个标签来更改其文本。 在tkinter中非常容易,这里不是这样。 我尝试了许多不同的方法来使它正常工作,但是失败了。 您会注意到该按钮调用了tick()函数,它最初是在切换屏幕,但是我对其进行了更改,因此我可以通过它来更改标签。 最终,我希望时钟在屏幕顶部运行,但会决定将其更新为任意文本。 完整代码的回复将是超级!
(附言。我意识到你们中的大多数人可能会在30分钟内完成整个应用的编写,这是非常基本的)

谢谢

这是我的代码:

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   ")

该行会导致崩溃,因为尚未定义日期,但是您可以在其自己的定义中使用它。

self.date_label(text=date + time2)

self.date_label是Label实例,但不可调用。 设置kivy属性的正确方法是将其作为属性访问:

self.date_label.text = time2

(由于定义中存在上述问题,我删除了日期部分)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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