gpt4 book ai didi

python - 更新 kivy 上的标签

转载 作者:行者123 更新时间:2023-12-04 10:07:25 26 4
gpt4 key购买 nike

我想通过来自 Screen1 的输入更新 kivy 上的标签并在 Screen3 上显示。在 Screen1 我在 textinput id:slot 输入,'test' 是全局变量,从中获取变量。当我尝试在 Screen3 上做时。 'test' 它可以从 Screen1 获取变量,但它不会在 Screen3 上更新。
我该如何解决?如果我做错了什么,请给我建议。我是初学者。
这是我的代码
.py 代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen,NoTransition
from kivy.properties import ObjectProperty,StringProperty,NumericProperty
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.clock import Clock
import time
import mysql.connector as mariadb
import sys
Window.size=(800,480)
Builder.load_file("mainmenu.kv")

test=''
class Mainscreen(Screen):
pass
class Screen1(Screen):
sslot=StringProperty(None)
sname=StringProperty(None)
sprice=NumericProperty(0)
samount=NumericProperty(0)
def __init__(self,**kwargs):
super(Screen1, self).__init__(**kwargs)
sslot=self.sslot
sname=self.sname
sprice=self.sprice
samount=self.samount
def setprice(self):
global test
if self.f_slot.text!='' and self.f_name.text!='' and self.f_price.text!=0 and self.f_amount.text!=0 :
sslot=str(self.f_slot.text)
sname=self.f_name.text
sprice=int(self.f_price.text)
samount=int(self.f_amount.text)
test=sslot

mariadb_connection = mariadb.connect(host='localhost', user='pop', password='raspberry', database='setprice')
cursor=mariadb_connection.cursor()
sql="UPDATE Setprice SET Name=%s,Price=%s,Amount=%s WHERE Slot=%s"
val=(sname,sprice,samount,sslot)
cursor.execute(sql,val)
mariadb_connection.commit()

self.ids.slot.text=""
self.ids.name.text=""
self.ids.price.text=""
self.ids.amount.text=""
self.ids.slot.focus=True
self.ids.name.focus=False
self.ids.price.focus=False
self.ids.amount.focus=False
else:
print("error")

class Screen2(Screen):
pass

class Screen3(Screen):
#This it what i can't do
fsales=StringProperty()
def __init__(self,**kwargs):
super(Screen3, self).__init__(**kwargs)
Clock.schedule_once(self.updatesales,0.1)
def updatesales(self,*args):
global test
self.fsales=test
print(self.fsales)

class Screen4(Screen):
pass

class Screen21(Screen):
pass

class Screen22(Screen):
pass

class Manager(ScreenManager):
screen_mainmenu=ObjectProperty(None)
screen_menu1=ObjectProperty(None)
screen_menu2=ObjectProperty(None)
screen_menu3=ObjectProperty(None)
screen_menu4=ObjectProperty(None)

class VendingmachineApp(App):
Menu3=Screen3()
def build(self):
m=Manager(transition=NoTransition())
return m

if __name__=="__main__":
VendingmachineApp().run()
.kv 代码:
<Mainscreen>:
BoxLayout:
orientation:'vertical'
spacing:2
padding:30
Label:
text:"Menu"
font_size:40
Button:
text:"Setprice & amount"
on_press:root.manager.current='screen_menu1'
font_size:30
Button:
text:"Motor"
on_press:root.manager.current='screen_menu2'
font_size:30
Button:
text:"Check sales"
on_press:
root.manager.current='screen_menu3'
app.Menu3.updatesales()
font_size:30

Button:
text:"Data log"
on_press:root.manager.current='screen_menu4'
font_size:30

<Screen1>:
f_slot:slot
f_name:name
f_price:price
f_amount:amount
enter:enter

FloatLayout:
Label:
text:'Setprice & amount'
font_size:40
size:600,150
size_hint:(None,None)
pos:(0,370)
Label:
text:'Slot : '
font_size:30
size:600,150
size_hint:(None,None)
pos:(-20,300)
TextInput:
id:slot
multiline:False
focus: True
input_filter:"int"
font_size:30
size:200,50
size_hint:(None,None)
pos:(450,350)
on_text_validate:
name.focus=True
Label:
text:'Name : '
font_size:30
size:600,150
size_hint:(None,None)
pos:(-10,220)
TextInput:
id:name
focus: False
multiline:False
font_size:30
size:200,50
size_hint:(None,None)
pos:(450,270)
on_text_validate:
price.focus=True
Label:
text:'Price : '
font_size:30
size:600,150
size_hint:(None,None)
pos:(-15,140)
TextInput:
id:price
multiline:False
input_filter:"int"
focus: False
font_size:30
size:200,50
size_hint:(None,None)
pos:(450,190)
on_text_validate:
amount.focus=True
Label:
text:'Amount : '
font_size:30
size:600,150
size_hint:(None,None)
pos:(0,60)
TextInput:
id:amount
multiline:False
input_filter:"int"
font_size:30
focus: False
size:200,50
size_hint:(None,None)
pos:(450,110)
on_text_validate:root.setprice()
Button:
id:back
background_normal:'back.png'
size:100,93
size_hint:(None,None)
pos:(80,20)
on_press:root.manager.current='screen_mainmenu'
Button:
id:enter
text:'Enter'
size:250,50
size_hint:(None,None)
pos:(430,30)
font_size:30
focus:False
on_press:root.setprice()

<Screen2>:
FloatLayout:
Label:
text:'Motor'
font_size:40
size:600,150
size_hint:(None,None)
pos:(-140,370)
Button:
text:"Motor log"
#on_press:root.manager.current='screen_'
size:600,120
size_hint:(None,None)
font_size:30
pos:(100,275)
Button:
text:"Test Motor"
#on_press:root.manager.current='screen_three'
size:600,120
size_hint:(None,None)
font_size:30
pos:(100,150)
Button:
background_normal:'back.png'
size:100,93
size_hint:(None,None)
pos:(80,20)
on_press:root.manager.current='screen_mainmenu'

<Screen3>
id:Menu3
FloatLayout:
Label:
text:'Check sales'
font_size:40
size:600,150
size_hint:(None,None)
pos:(-90,350)
Label:
text:'Total sales : '
font_size:30
size:250,100
size_hint:(None,None)
pos:(100,300)
Label:
id:ttsales
text:Menu3.fsales
font_size:30
size:250,100
size_hint:(None,None)
pos:(100,350)
Button:
text:'More'
font_size:20
size:80,60
size_hint:None,None
pos:(500,320)
Button:
text:'Clear sales'
font_size:30
size:600,100
size_hint:(None,None)
pos:(100,200)
Button:
background_normal:'back.png'
size:100,93
size_hint:(None,None)
pos:(80,20)
on_press:root.manager.current='screen_mainmenu'
<Screen4>
FloatLayout:
Label:
text:'Data log'
font_size:40
size:600,150
size_hint:(None,None)
pos:(-125,350)
Button:
text:'Sales report'
font_size:30
size:600,100
size_hint:(None,None)
pos:(100,280)
Button:
text:'Hot product sales'
font_size:30
size:600,100
size_hint:(None,None)
pos:(100,170)
Button:
background_normal:'back.png'
size:100,93
size_hint:(None,None)
pos:(80,20)
on_press:root.manager.current='screen_mainmenu'

<Manager>:
id:screen_manager
screen_mainmenu:screen_mainmenu
screen_menu1:screen_menu1
screen_menu2:screen_menu2
screen_menu3:screen_menu3
screen_menu4:screen_menu4
screen_menu21:screen_menu21
screen_menu22:screen_menu22
Mainscreen:
id:screen_mainmenu
name:'screen_mainmenu'
manager:screen_manager
Screen1:
id:screen_menu1
name:'screen_menu1'
manager:screen_manager
Screen2:
id:screen_menu2
name:'screen_menu2'
manager:screen_manager
Screen3:
id:screen_menu3
name:'screen_menu3'
manager:screen_manager
Screen4:
id:screen_menu4
name:'screen_menu4'
manager:screen_manager
Screen21:
id:screen_menu21
name:'screen_menu21'
manager:screen_manager
Screen22:
id:screen_menu22
name:'screen_menu22'
manager:screen_manager

最佳答案

因此,如果您希望在 Screen3 中发生某些事情在 Screen1 工作时- 这是您实现它的方法。

self.manager.get_screen('screen3').ids.labelid.text = 'NewText'

上面的示例代码可以在 textinput的情况下的 screen1

关于python - 更新 kivy 上的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61510286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com