我有一个kivy程序,我需要标签来填充屏幕类中某个函数提供的数据。它可以正常使用更新按钮,但是我也希望它在加载时填充标签。这是python文件和kv文件:
py:
from kivy import *
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
import requests
from bs4 import BeautifulSoup
class CurrentHighScores(Screen):
highscore = None
highscore_two = None
highscore_three = None
highscore_four = None
highscore_five = None
label_start_one = StringProperty(' ')
label_start_two = StringProperty(' ')
label_start_three = StringProperty(' ')
label_start_four = StringProperty(' ')
label_start_five = StringProperty(' ')
def speed_scraper(self):
URL = 'http://algoodspeeders.org/alpr/speederid.php'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
indexes = [3, 5]
speed_data = []
for tbody in soup.findAll('tbody'):
for tr in tbody.findAll('tr'):
speed_values = [td.text for td in tr.findAll('td')]
speed_vals = [speed_values[x] for x in indexes]
speed_data.append(speed_vals)
speed_data.sort(reverse=True)
# print(speed_data[0:5])
self.highscore = speed_data[0]
self.highscore_two = speed_data[1]
self.highscore_three = speed_data[2]
self.highscore_four = speed_data[3]
self.highscore_five = speed_data[4]
self.ids.high_one.text = '1st: ' + str(self.highscore[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore[1])
self.ids.high_two.text = '2nd: ' + str(self.highscore_two[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_two[1])
self.ids.high_three.text = '3rd: ' + str(self.highscore_three[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_three[1])
self.ids.high_four.text = '4th: ' + str(self.highscore_four[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_four[1])
self.ids.high_five.text = '5th: ' + str(self.highscore_five[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_five[1])
print('High Scores List')
print('----------------')
print('1st: ' + str(self.highscore[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore[1]))
print('2nd: ' + str(self.highscore_two[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_two[1]))
print('3rd: ' + str(self.highscore_three[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_three[1]))
print('4th: ' + str(self.highscore_four[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_four[1]))
print('5th: ' + str(self.highscore_five[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_five[1]))
class ScreenManage(ScreenManager):
pass
kv = Builder.load_file('algoodspeed.kv')
class AlgoodSpeedApp(App):
def build(self):
return kv
if __name__ == '__main__':
AlgoodSpeedApp().run()
.kv:
ScreenManage:
CurrentHighScores:
<CurrentHighScores>
name: 'CurrentHighScores'
GridLayout:
cols: 1
Label:
name: 'current_high_scores'
text: 'Current High Speeds For Cooper Rd:'
font_size: 30
Label:
id: high_one
text: root.label_start_one
Label:
id: high_two
text: root.label_start_two
Label:
id: high_three
text: root.label_start_three
Label:
id: high_four
text: root.label_start_four
Label:
id: high_five
text: root.label_start_five
Button:
id: update
text: 'Update Current High Speeds'
on_release:
root.speed_scraper()
我尝试使用时钟之类的方法来延迟类之外的函数调用,但这完全没有帮助。感谢您的帮助!
答案 0 :(得分:0)
感谢对我的问题的评论,我做了一些进一步的研究,并对代码进行了稍微的修改,这是我对遇到此问题的其他人的发现。我使用on_pre_enter调用函数,并且在函数内只需更新“标签”变量,就必须更改kv文件中的文本。这是它的外观。我保留了一些旧代码,但将其注释掉了,以便您可以看到以前的样子:
.py:
from kivy import *
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
import requests
from bs4 import BeautifulSoup
class CurrentHighScores(Screen):
def on_pre_enter(self, *args):
self.speed_scraper()
highscore = None
highscore_two = None
highscore_three = None
highscore_four = None
highscore_five = None
label_start_one = StringProperty(' ')
label_start_two = StringProperty(' ')
label_start_three = StringProperty(' ')
label_start_four = StringProperty(' ')
label_start_five = StringProperty(' ')
def speed_scraper(self):
URL = 'http://algoodspeeders.org/alpr/speederid.php'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
indexes = [3, 5]
speed_data = []
for tbody in soup.findAll('tbody'):
for tr in tbody.findAll('tr'):
speed_values = [td.text for td in tr.findAll('td')]
speed_vals = [speed_values[x] for x in indexes]
speed_data.append(speed_vals)
speed_data.sort(reverse=True)
# print(speed_data[0:5])
self.highscore = speed_data[0]
self.highscore_two = speed_data[1]
self.highscore_three = speed_data[2]
self.highscore_four = speed_data[3]
self.highscore_five = speed_data[4]
self.label_start_one = ('1st: ' + str(self.highscore[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore[1]))
self.label_start_two = ('2nd: ' + str(self.highscore_two[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_two[1]))
self.label_start_three = ('3rd: ' + str(self.highscore_three[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_three[1]))
self.label_start_four = ('4th: ' + str(self.highscore_four[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_four[1]))
self.label_start_five = ('5th: ' + str(self.highscore_five[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_five[1]))
'''self.ids.high_one.text = '1st: ' + str(self.highscore[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore[1])
self.ids.high_two.text = '2nd: ' + str(self.highscore_two[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_two[1])
self.ids.high_three.text = '3rd: ' + str(self.highscore_three[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_three[1])
self.ids.high_four.text = '4th: ' + str(self.highscore_four[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_four[1])
self.ids.high_five.text = '5th: ' + str(self.highscore_five[0]) + ' MPH | ' + 'License Plate No. ' + str(
self.highscore_five[1])
print('High Scores List')
print('----------------')
print('1st: ' + str(self.highscore[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore[1]))
print('2nd: ' + str(self.highscore_two[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_two[1]))
print('3rd: ' + str(self.highscore_three[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_three[1]))
print('4th: ' + str(self.highscore_four[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_four[1]))
print('5th: ' + str(self.highscore_five[0]) + ' MPH | ' + 'License Plate No. ' + str(self.highscore_five[1]))
'''
class ScreenManage(ScreenManager):
pass
kv = Builder.load_file('algoodspeed.kv')
class AlgoodSpeedApp(App):
def build(self):
return kv
if __name__ == '__main__':
AlgoodSpeedApp().run()
.kv
ScreenManage:
CurrentHighScores:
<CurrentHighScores>
name: 'CurrentHighScores'
on_enter: print('working')
GridLayout:
cols: 1
Label:
name: 'current_high_scores'
text: 'Current High Speeds For Cooper Rd:'
font_size: 30
Label:
id: high_one
text: root.label_start_one
Label:
id: high_two
text: root.label_start_two
Label:
id: high_three
text: root.label_start_three
Label:
id: high_four
text: root.label_start_four
Label:
id: high_five
text: root.label_start_five
Button:
id: update
text: 'Update Current High Speeds'
on_release:
root.speed_scraper()