编辑:所以我回答了我的第一个问题,这个问题在Kivy文档中是正确的:-)。 基本上我需要为mygridwid_widget:mygridwid添加一个KV文件的propoerty,然后将其传递给我的方法get_apptime,就像这样
Switch:
id: startbutton
# text: "Start / Stop"
# size_hint_x: 1
on_active: root.get_apptime(self, self.active, mygridwid)
现在一切都编译好了,我可以看到系统在激活交换机时正在调用更新方法。现在我的问题是UI似乎没有重新绘制更新的网格,但是当我运行调试器时,我可以看到网格已经创建并填充了数据。任何人都知道我做错了什么?
我是Kivy的新手,对Python有点缺乏经验。我的问题是我正在为一个项目制作一个简单的GUI。在我的GUI中,我需要一个按钮,打开系统,跟踪测试时间,然后只要主开关打开,就用数据更新网格布局。"当它关闭时,它不应该更新按钮。现在我只是尝试对主UI元素进行原型设计,后者将连接我的MQTT代码以将数据推送到系统。
在下面的代码中,我特别想知道如何在BDdemoForm类的get_apptime方法中调用MyGrid类中的update_data方法:
def get_apptime(self, instance, value):
myappstate = value
if myappstate == True:
self.bsdemo_currenttime = time.asctime()
#how do i call this method in this class
root.MyGrid.update_display()
print(myappstate)
else:
self.bsdemo_currenttime = "0"
print(myappstate)
无论如何这里是我的代码,main.py:
import time
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.properties import BooleanProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
#custom and helper classes
import labelb
import DeviceData
#import AppState
class TableHeader(Label):
pass
class PlayerRecord(Label):
pass
class BSdemoRoot(BoxLayout):
pass
myappstate = BooleanProperty()
myappstate = False
#mygridob = MyGrid()
# class my widget
class BSdemoForm(BoxLayout):
bsdemo_currenttime = StringProperty()
device_data = ObjectProperty()
def __init__(self, **kwargs):
super(BSdemoForm, self).__init__(**kwargs)
self.bsdemo_currenttime = "0"
def get_apptime(self, instance, value):
myappstate = value
if myappstate == True:
self.bsdemo_currenttime = time.asctime()
root.MyGrid.update_display()
#MyGrid(self).update_display()
print(myappstate)
else:
self.bsdemo_currenttime = "0"
print(myappstate)
# sys_data = self.get_devicedata()
#create a data manager class
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.get_tableheaders()
self.get_devicedata()
self.display_data()
def get_tableheaders(self):
self.tabledata = [
{'LRT': 'Last Recived Time', 'Type': 'Classification Type', 'STR': 'Session Time Remaining'},
]
def get_devicedata(self):
self.data = [
{'LRT': '8.243', 'Type': 'DJI Mavic', 'STR': '4.39'},
{'LRT': '5.243', 'Type': 'Parrot BeBop', 'STR': 'Close'},
{'LRT': '7.11', 'Type': 'Parrot Ebee', 'STR': 'Close'},
{'LRT': '5.3', 'Type': 'Parrot DISCO', 'STR': '11.12'},
{'LRT': '3.20', 'Type': 'Yuneec Typhoon', 'STR': '2.13'},
{'LRT': '5.44', 'Type': 'Yuneec', 'STR': '5.23'},
{'LRT': '1.12', 'Type': 'DJI Pro 4', 'STR': '1.10'},
{'LRT': '2', 'Type': 'Parrot Sumo', 'STR': '2.3555'}
]
# assembles the items to be pushed into the gridlayout widget
def display_data(self):
self.clear_widgets()
print(myappstate)
for myi in xrange(len(self.tabledata)):
print("display data method was run")
myrow = self.create_header(myi)
for myitem in myrow:
self.add_widget(myitem)
if myappstate == True:
for i in xrange(len(self.data)):
row = self.create_player_info(i)
#add all the items to the widget
for item in row:
self.add_widget(item)
@classmethod
def update_display(self):
self.clear_widgets()
print(myappstate)
for myi in xrange(len(self.tabledata)):
print("update data display was called")
myrow = self.create_header(myi)
for myitem in myrow:
self.add_widget(myitem)
if myappstate == True:
for i in xrange(len(self.data)):
row = self.create_player_info(i)
#add all the items to the widget
for item in row:
self.add_widget(item)
# this allows you to change the tableheader column names and number
def create_header(self,i):
first_column = TableHeader(text=self.tabledata[i]['LRT'])
second_column = TableHeader(text=self.tabledata[i]['Type'])
third_column = TableHeader(text=self.tabledata[i]['STR'])
return [first_column, second_column, third_column]
# puts the data into the right column to build the table
def create_player_info(self, i):
first_column = PlayerRecord(text=self.data[i]['LRT'])
second_column = PlayerRecord(text=self.data[i]['Type'])
third_column = PlayerRecord(text=self.data[i]['STR'])
return [first_column, second_column, third_column]
class BSKivyApp(App):
pass
if __name__ == '__main__':
BSKivyApp().run()
这是我的kv文件BSKivy.ky。
#: import ListItemLabel kivy.uix.listview.ListItemLabel
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
<PlayerRecord>:
id: myplayer
size_hint_y: None
height: '30dp'
width: '100dp'
canvas.before:
Color:
rgb: 0.2, 0.2, 0.2
Rectangle:
pos: self.pos
size: self.size
<TableHeader>:
id: myheader
size_hint_y: None
height: '30dp'
width: '100dp'
canvas.before:
Color:
rgb: 0.5, 0.5, 0.5
Rectangle:
pos: self.pos
size: self.size
BSdemoRoot:
<BSdemoRoot>:
BSdemoForm
<BSdemoForm>
orientation: "vertical"
#device_data: device_data_results
BoxLayout:
orientation: "vertical"
BoxLayout:
Label:
text: "BS Demo Interface and GUI"
BoxLayout:
Label:
text: "Current Time"
size_hint_x: 1
Label:
id: app_runtime_label
text: root.bsdemo_currenttime
size_hint_x: 1
Switch:
id: startbutton
# text: "Start / Stop"
# size_hint_x: 1
on_active: root.get_apptime(self, self.active)
# ; app.root.MyGrid.display_data(self)
ScrollView:
size_hint_y: None
height: '200dp'
MyGrid:
id: mygridwid
cols: 3
size_hint_y: None
height: self.minimum_height
spacing: '1dp'
BoxLayout:
Label:
text: "replace with a button"
答案 0 :(得分:0)
所以我开始工作并关闭它。基本上,我有两个问题。一个人不理解如何正确使用属性和ID。我的第二个问题是一个简单的变量范围问题。