【发布时间】:2021-07-21 03:08:33
【问题描述】:
a.py
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.app import App
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from enum import Enum
from enum import auto
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
import threading
class menu(Enum):
Espresso = (0)
Latte = (1)
Sahlep = (2)
TurkKahvesi = (3)
def __init__(self, id):
self.id = id
class Test(Screen):
pass
class MainWindow(Screen):
pass
class Third(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class Tabs(TabbedPanel):
pass
class Labs():
pass
class CountSigns(StackLayout):
my_text = 0
count = 0
def on_button_click_plus(self):
print("Button clicked plus")
self.count += 1
self.my_text = str(self.count)
print(self.my_text)
return self.my_text
def on_button_click_minus(self):
print("Button clicked minus")
self.my_text = str(self.count)
if self.count > 0:
self.count -= 1
else:
pass
def __init__(self, **kwargs):
value = str(self.my_text)
super().__init__(**kwargs)
b = Button(text='latte', size_hint=(None, None), size=(100, 100))
self.add_widget(b)
b.bind(on_press=lambda x: self.on_button_click_plus())
lab = Label(text= str(self.my_text), color= (1, .5, 1, 1), size=(50, 100), size_hint=(None, None))
self.add_widget(lab)
btn1 = Button(text='-', size_hint=(None, None), size=(50, 100))
btn1.bind(on_press=lambda x: self.on_button_click_minus())
self.add_widget(btn1)
class WidgetsExample(CountSigns):
value = CountSigns.my_text
class WidgetsExample2(GridLayout):
def __init__(self,**kwargs):
super(WidgetsExample2,self).__init__(**kwargs)
self.top_grid = GridLayout() # Widget above main widget to hold all text and input boxes.
self.cols = 2 # no.of columns
kv = Builder.load_file("b.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
b.kv
WindowManager:
MainWindow:
Third:
<MainWindow>:
name: "main"
GridLayout:
cols:2
Button:
text: "New Order"
on_release:
app.root.current = "third"
root.manager.transition.direction = "left"
<Third>:
name: "third"
Tabs:
do_default_tab: False
TabbedPanelItem:
text: 'Hot Drinks'
CountSigns:
因此,当我运行此代码时,“实验室”中的 my_text 不会在 GUI 中更新。然而,打印显示 my_text 实际上发生了变化。我怎样才能使标签更新。我基本上希望按钮本身使值上升,减号按钮使值下降。当我的代码在 kv 文件中时它确实有效,但是因为我需要存储我决定需要在 python 文件中定义这些值的值。
【问题讨论】: