如何在 kivy 中更改标签文本?
How can I make the label text change in kivy?
我正在尝试编写一个简单的预算应用程序,但在实际程序中显示变量时遇到了一些困难。此外,在成功更改存款字典中的新总预算金额后(通过 class EditMenu 的 submit_total_change 方法中的那个小字样(存款['total'])行确认),它赢了在重新启动整个程序之前在应用程序上显示。
有人知道如何解决这个问题吗?
而且我也愿意接受任何改进建议,我几天前才开始编程。
提前致谢!!!
test.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()
mymain.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
似乎是在 MainScreen
和 EditMoney
class 中定义的。由于 kv
中的 ids
仅在作为规则根的 class 中定义,因此那些 ids
仅在 MainScreen
和 EditMoney
classes.
您应该能够通过访问那些 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.root.ids. . .
我正在尝试编写一个简单的预算应用程序,但在实际程序中显示变量时遇到了一些困难。此外,在成功更改存款字典中的新总预算金额后(通过 class EditMenu 的 submit_total_change 方法中的那个小字样(存款['total'])行确认),它赢了在重新启动整个程序之前在应用程序上显示。
有人知道如何解决这个问题吗? 而且我也愿意接受任何改进建议,我几天前才开始编程。 提前致谢!!!
test.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()
mymain.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
似乎是在 MainScreen
和 EditMoney
class 中定义的。由于 kv
中的 ids
仅在作为规则根的 class 中定义,因此那些 ids
仅在 MainScreen
和 EditMoney
classes.
您应该能够通过访问那些 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.root.ids. . .