簡體   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