簡體   English   中英

如何使 kivy 中的 label 文本更改?

[英]How can I make the label text change in kivy?

我正在嘗試編寫一個簡單的預算應用程序,但在實際程序中顯示變量時遇到了一些困難。 同樣在成功更改存款字典中的新總預算金額后(通過 class EditMenu 的 submit_total_change 方法中的那個小字(deposit['total'])行確認),它不會在重新啟動之前顯示在應用程序上程序。

有人知道如何解決這個問題嗎? 我也願意接受任何改進建議,我幾天前才開始編程。 非常感謝提前!!!

測試.py

#other imports
import numpy as np
from looking import *
import threading
import time

#kivy imports
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.clock import Clock

#useful function for later
def save_list():
    np.save("budget_list.npy", budget)
    print("Saved budget list changes")

def save_list2():
    np.save("deposit.npy", deposit)
    print("Saved deposit changes")



    
#budget dictionaries
budget = dict()
deposit = dict()

#load budget
def load_budget():
    global budget
    loading = np.load("budget_list.npy", allow_pickle='True').item()
    budget = loading
    
load_budget()


#load deposit
def load_deposit():
    global deposit
    loading2 = np.load("deposit.npy", allow_pickle='True').item()
    deposit = loading2
    for k in deposit: print(k, deposit[k])

load_deposit()

#useful variables
tdeposit_money = deposit['total']
tspend_money = looking(budget).allprice()
totalmoney = tdeposit_money - tspend_money

#class to handle the main menu
class MainMenu():

    def __init__(self, **kwargs):
        pass

    def print_deposit_total(self):

        self.total_deposit = deposit['total']

        return str(self.total_deposit)

    def print_total_money(self):

        self.total_money = totalmoney

        return str(self.total_money)
    
    def exitt(self):
        quit()

    


#class to handle edit money menu
class EditMenu():

    def __init__(self, **kwargs):
        pass
    
    def display_money_left_cat (self):

        self.deposit = deposit
        self.display = str()

        for k in self.deposit:
            if k != 'total':
                print("Category:", k)
                print("Assigned Money:", self.deposit[k])
                self.display += "Category: "+str(k)+" Assigned Money: "+str(self.deposit[k])+" Money left: "+ str(deposit[k] - looking(budget).price_category(k))+"\n"

        return str(self.display)

    def display_total_money_deposit(self):
        self.deposit_total = deposit['total']

        return str(self.deposit_total)


    def submit_total_change(self):

        deposit['total'] = int(self.ids.new_total.text)
        self.ids.new_total.text = ""
        save_list2()

        #self.ids.lbl2.text = "Total money in deposit: "+ str(deposit['total'])
        #self.ids.lbl3.text = "Total money left: "+ str(totalmoney)
        print(deposit['total'])
        #self.ids.lbl1_1.text = "Total money in deeposit: "+ str(deposit['total'])
        #self.ids.lbl1_1.text = "Current total money in deposit: "+ str(deposit['total'])
        

                
                
#MainMenu Screen
class MainScreen(Screen, GridLayout, MainMenu):
    
    #for screen update
    def routine(self): 
     self.parent.current = 'menu' 
     threading.Thread(target=self.manager.get_screen('menu').build).start()


#Edit Money Menu Screen
class EditMoney(Screen, GridLayout,MainMenu, EditMenu):
    pass

class EditMoney_Amount_T(Screen, GridLayout, EditMenu):
    pass

class EditMoney_Amount_C(Screen, GridLayout, EditMenu):
    pass


#Add List Menu Screen
class AddList(Screen, GridLayout):
    pass

class EditList(Screen, GridLayout):
    pass

    
        
class MyMainApp(App):
    def build(self):
        
        sm = ScreenManager()
        #MainMenu Screen
        sm.add_widget(MainScreen(name='menu'))

        #Edit Money Menu Screen
        sm.add_widget(EditMoney(name='edit_m'))
        sm.add_widget(EditMoney_Amount_T(name='edit_m_a_t'))
        sm.add_widget(EditMoney_Amount_C(name='edit_m_a_c'))

        #Add List Menu Screen
        sm.add_widget(AddList(name='add_b'))

        #Edit List Menu Screen
        sm.add_widget(EditList(name='edit_b'))
        
        return sm

if __name__ == "__main__":
    MyMainApp().run()

我的main.kv

<MainScreen>:   
    GridLayout:
        cols: 1
        size: root.width, root.height
        
        Label:
            id: lbl1
            text: "BUDGET APP"
            font_size: 50
            
        Label:
            id: lbl2
            text: "Total money in deposit: "+ root.print_deposit_total()
            font_size: 30
            
        Label:
            id: lbl3
            text: "Total money left: "+ root.print_total_money()
            font_size: 30
        
        GridLayout:
            cols: 3
            
            Button:
                id: b1
                text: "Add/Edit your available money"
                on_press: 
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'edit_m'
                
            Button:
                id: b2
                text: "Add new item to the budget list"
                on_press: 
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'add_b'
                
            Button:
                id: b3
                text: "Edit items in budget list"
                on_press: 
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'edit_b'
                
        Button:
            id: b4
            text: "Exit"
            font_size: 40
            on_press: root.exitt()
    
    
<EditMoney>:    
    GridLayout:
        cols: 1
        
        size: 50, 50
        
        Label:
            id: lbl1_1
            text: "Total money in deposit: "+ root.display_total_money_deposit()
            font_size: 30
            pos: 0, 1
            
        Label:
            id: lbl1_2 
            text: "Category:"
            font_size: 30
            
        GridLayout:
            cols: 2
            
            Button:
                id: b1_1
                text: "Change total available money"
                on_press:
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'edit_m_a_t'
                    
            Button:
                id: b1_2
                text: "Change money assinged to category"
                on_press:
                    root.manager.transition.direction = 'left'
                    root.manager.current =  'edit_m_a_c'
            
        Button:
            id: b1_1
            text: "Back to main menu"
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'menu'
                
<EditMoney_Amount_T>:
    new_total: new_total
    
    GridLayout:
        cols: 1
        
        Label:
            id: lbl1_3
            text: "Current assigned total money: "+ root.display_total_money_deposit()
        
        GridLayout:
            cols: 2
            
            Label:
                id: lbl1_4
                text: "New amount: "
                
            TextInput:
                id: new_total
                multiline: False
                
        Button:
            id: b1_1_3
            text: "Submit"
            on_press: 
                root.submit_total_change()
                root.manager.transition.direction = 'right'
                root.manager.current = 'edit_m'
                
        Button:
            id: b1_1_4
            text: "Back"
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'edit_m'
                
<EditMoney_Amount_C>:
    GridLayout:
        cols: 1

        
        size: 50, 50
        
        Label:
            id: lbl1_2_1
            text: root.display_money_left_cat()
            font_size: 30
            
        Button:
            id: b1_2_1
            text: "Change"
            on_press: root.display_money_left_cat()
                    
        Button:
            id: b1_2_2
            text: "Back"
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'edit_m'



<AddList>:
    GridLayout:
        cols: 1
        
        size: 50, 50
        
        Label:
            text: "Add items to the budget list"
            font_size: 100
            
        Button:
            id: b2_1
            text: "Back to main menu"
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'menu'

<EditList>:
    GridLayout:
        cols: 1
        
        size: 50, 50
        
        Label:
            text: "Edit budget list"
            font_size: 100
            
        Button:
            id: b3_1
            text: "Back to main menu"
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'menu'

您在submit_total_change()方法中的注釋代碼:

    #self.ids.lbl2.text = "Total money in deposit: "+ str(deposit['total'])
    #self.ids.lbl3.text = "Total money left: "+ str(totalmoney)
    print(deposit['total'])
    #self.ids.lbl1_1.text = "Total money in deeposit: "+ str(deposit['total'])
    #self.ids.lbl1_1.text = "Current total money in deposit: "+ str(deposit['total'])

正在嘗試引用未在包含 class ( self ) 中定義的ids 這些ids似乎是在MainScreenEditMoney類中定義的。 由於來自kvids僅在作為規則根的 class 中定義,因此這些ids僅在MainScreenEditMoney類中可用。

您應該能夠通過訪問那些 class 實例並通過這些 class 實例引用ids來引用它們,如下所示:

def submit_total_change(self):

    deposit['total'] = int(self.ids.new_total.text)
    self.ids.new_total.text = ""
    save_list2()

    print(deposit['total'])

    # get a reference to the ScreenManager
    sm = App.get_running_app().root

    # Get references to the screens that contain the ids of interest
    mainscreen = sm.get_screen('menu')
    editmoney = sm.get_screen('edit_m')

    # Now change the Label texts
    mainscreen.ids.lbl2.text = "Total money in deposit: "+ str(deposit['total'])
    mainscreen.ids.lbl3.text = "Total money left: "+ str(totalmoney)
    editmoney.ids.lbl1_1.text = "Total money in deeposit: "+ str(deposit['total'])
    

而不是self.ids. . . self.ids. . . 嘗試self.root.ids. . . self.root.ids. . .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM