Почему на ПК мигает и со временем выбивает GUI Kivy?
Есть такой код: main:
import time
from threading import Thread
from kivy.app import App
from kivy.properties import Clock
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen
import Variables
import streams
class MenuScreen(Screen):
def __init__(self, **kw):
super(MenuScreen, self).__init__(**kw)
colorbg1 = [0.145, 0.592, 0.933, 1]
colorbg2 = [0.11, 0.894, 0.894, 1]
colorbg3 = [0.965, 0.02, 0.337, 1]
Clock.schedule_interval(lambda dt: self.update1(), 1)
self.button1 = Button(text='Server', size_hint=(1.5, 1), background_color=(colorbg1))
self.button2 = Button(text='Status', size_hint=(0.2, 1), background_color=(colorbg2))
self.button3 = Button(text='Connect', size_hint=(0.2, 1))
self.button4 = Button(text='Reconnect', size_hint=(0.2, 1))
self.button5 = Button(text='Internet', size_hint=(0.4, 1), background_color=(colorbg1))
self.button6 = Button(text='Status', background_color=(colorbg2))
self.button7 = Button(text='')
self.button8 = Button(text='')
self.button9 = Button(text='Control', size_hint=(0.4, 1), background_color=(colorbg1))
self.button10 = Button(text='Status', size_hint=(0.2, 1), background_color=(colorbg2))
self.button11 = Button(text='Remote', size_hint=(0.2, 1))
self.button12 = Button(text='Auto', size_hint=(0.2, 1))
self.button13 = Button(text='Power', size_hint=(0.4, 1), background_color=(colorbg1))
self.button14 = Button(text='Status', background_color=(colorbg2))
self.button15 = Button(text='')
self.button16 = Button(text='')
self.button17 = Button(text='Tank state', size_hint=(1.5, 1), background_color=colorbg1)
self.button18 = Button(text='Status', size_hint=(0.2, 1), background_color=colorbg2)
self.button19 = Button(text='', size_hint=(0.2, 1))
self.button20 = Button(text='', size_hint=(0.2, 1))
self.button21 = Button(text='Pump state', size_hint=(0.4, 1), background_color=colorbg1)
self.button22 = Button(text='Status', background_color=(colorbg2))
self.button23 = Button(text='On')
self.button24 = Button(text='Off')
self.button25 = Button(text='Light sensor', size_hint=(1.5, 1), background_color=(colorbg1))
self.button26 = Button(text='Status', size_hint=(0.2, 1), background_color=(colorbg2))
self.button27 = Button(text='', size_hint=(0.2, 1))
self.button28 = Button(text='', size_hint=(0.2, 1))
self.button29 = Button(text='Sadok Relay', size_hint=(0.4, 1), background_color=(colorbg1))
self.button30 = Button(text='Status', background_color=(colorbg2))
self.button31 = Button(text='On')
self.button32 = Button(text='Off')
self.button33 = Button(text='Rear Relay', size_hint=(0.4, 1), background_color=(colorbg1))
self.button34 = Button(text='Status', background_color=(colorbg2))
self.button35 = Button(text='On')
self.button36 = Button(text='Off')
self.button37 = Button(text='4Relay 1', size_hint=(1.5, 1), background_color=(colorbg1))
self.button38 = Button(text='Status', size_hint=(0.2, 1), background_color=(colorbg2))
self.button39 = Button(text='On', size_hint=(0.2, 1))
self.button40 = Button(text='Off', size_hint=(0.2, 1))
self.button41 = Button(text='4Relay 2', size_hint=(0.4, 1), background_color=(colorbg1))
self.button42 = Button(text='Status', background_color=(colorbg2))
self.button43 = Button(text='On')
self.button44 = Button(text='Off')
self.button45 = Button(text='4Relay 3', size_hint=(1.5, 1), background_color=(colorbg1))
self.button46 = Button(text='Status', size_hint=(0.2, 1), background_color=(colorbg2))
self.button47 = Button(text='On', size_hint=(0.2, 1))
self.button48 = Button(text='Off', size_hint=(0.2, 1))
self.button49 = Button(text='4Relay 4', size_hint=(0.4, 1), background_color=(colorbg1))
self.button50 = Button(text='Status', background_color=(colorbg2))
self.button51 = Button(text='On')
self.button52 = Button(text='Off')
layout = GridLayout(cols=4, rows=13)
self.add_widget(layout)
layout.add_widget(self.button1)
layout.add_widget(self.button2)
layout.add_widget(self.button3)
layout.add_widget(self.button4)
layout.add_widget(self.button5)
layout.add_widget(self.button6)
layout.add_widget(self.button7)
layout.add_widget(self.button8)
layout.add_widget(self.button9)
layout.add_widget(self.button10)
layout.add_widget(self.button11)
layout.add_widget(self.button12)
layout.add_widget(self.button13)
layout.add_widget(self.button14)
layout.add_widget(self.button15)
layout.add_widget(self.button16)
layout.add_widget(self.button17)
layout.add_widget(self.button18)
layout.add_widget(self.button19)
layout.add_widget(self.button20)
layout.add_widget(self.button21)
layout.add_widget(self.button22)
layout.add_widget(self.button23)
layout.add_widget(self.button24)
layout.add_widget(self.button25)
layout.add_widget(self.button26)
layout.add_widget(self.button27)
layout.add_widget(self.button28)
layout.add_widget(self.button29)
layout.add_widget(self.button30)
layout.add_widget(self.button31)
layout.add_widget(self.button32)
layout.add_widget(self.button33)
layout.add_widget(self.button34)
layout.add_widget(self.button35)
layout.add_widget(self.button36)
layout.add_widget(self.button37)
layout.add_widget(self.button38)
layout.add_widget(self.button39)
layout.add_widget(self.button40)
layout.add_widget(self.button41)
layout.add_widget(self.button42)
layout.add_widget(self.button43)
layout.add_widget(self.button44)
layout.add_widget(self.button45)
layout.add_widget(self.button46)
layout.add_widget(self.button47)
layout.add_widget(self.button48)
layout.add_widget(self.button49)
layout.add_widget(self.button50)
layout.add_widget(self.button51)
layout.add_widget(self.button52)
# box.add_widget(Button(text='Two', on_press=lambda x: set_screen('add_food')))
def update1(self):
Clock.schedule_interval(lambda dt: one(self), 1)
def one(self):
self.button2.text = str(Variables.status_code_server_connections)
self.button6.text = str(Variables.state_internet_connections)
self.button10.text = str(Variables.change_recive)
self.button14.text = str('in work')
self.button18.text = str('in work')
self.button22.text = str('in work')
self.button26.text = str(Variables.receive_from_server1)
self.button30.text = str(Variables.receive_from_server2)
self.button34.text = str('in work')
self.button38.text = str(Variables.receive_from_server3)
self.button42.text = str(Variables.receive_from_server4)
self.button46.text = str(Variables.receive_from_server5)
self.button50.text = str(Variables.receive_from_server6)
class app1(App):
def build(self):
Clock.schedule_interval(lambda dt: self.update_time(), 1) # Call update time
# Clock.schedule_interval(lambda dt: self.pr(), 1)
return MenuScreen()
def update_time(self):
self.root.seconds_string = time.strftime("%S") # Update time
if __name__ == '__main__':
th2 = Thread(target=streams.start1, daemon=True)
th2.start()
th3 = Thread(target=streams.start2, daemon=True)
th3.start()
th4 = Thread(target=streams.check_Server_sensor_conections, daemon=True)
th4.start()
th5 = Thread(target=streams.check_req, daemon=True)
th5.start()
app1().run()
Скрипт logics:
logics:
import time
import Variables
def start(self, event):
return
def pumping():
i = 0
while i < 10:
Variables.pomp_state = i
i += 1
print(str(Variables.pomp_state))
time.sleep(1.0)
Скрипт streams:
import gc
import threading
import time
from time import sleep
import requests
from urllib3.exceptions import MaxRetryError, NewConnectionError, ResponseError
import Variables
lock = threading.RLock()
def start1():
while True:
try:
if Variables.status_code_server_connections == 200:
r4_0 = Variables.parsing_GPIO_4relay1
r4_1 = str(r4_0[0])
r4_2 = str(r4_0[1])
r4_3 = str(r4_0[2])
r4_4 = str(r4_0[3])
params = {'params': str(Variables.parsing_ESP1),
'params1': str(Variables.Sadok_Light1),
'params2_1': r4_1,
'params2_2': r4_2,
'params2_3': r4_3,
'params2_4': r4_4,
'control': 'home'}
print(params)
r = requests.get('http://f0555107.xsph.ru/index_mob.php', params=params, timeout=3.0)
Variables.counting_requaest += 1
r.encoding = "UTF8"
print('start1 = Ok')
print(r.text)
else:
print('start1 ' + Variables.time_now + ' Bad response, status_code= ' + str(
Variables.status_code_server_connections))
sleep(20.0)
except:
sleep(20.0)
continue
pass
sleep(20.0)
def start2():
while True:
try:
if Variables.status_code_server_connections == 200:
url = "http://f0555107.xsph.ru/hello.html"
r = requests.get(url, timeout=3.00)
Variables.counting_requaest += 1
r.encoding = "UTF8"
if r.status_code == 200:
print('start2 = Ok')
with open('response_server.html', 'w') as output_file:
output_file.write(r.text)
with open('response_server.txt', 'w') as output_file:
output_file.write(r.text)
text_file = open("response_server.html", "r")
lines = text_file.read().split(',')
print(lines)
# print(len(lines))
text_file.close()
Variables.receive_from_server = lines
Variables.receive_from_server1 = lines[0]
Variables.receive_from_server2 = lines[1]
Variables.receive_from_server3 = lines[2]
Variables.receive_from_server4 = lines[3]
Variables.receive_from_server5 = lines[4]
Variables.receive_from_server6 = lines[5]
Variables.receive_from_server7 = lines[6]
else:
Variables.state_exchange_data = r.status_code
# with open('controlling.txt', 'a') as output_file:
# output_file.write(Variables.time_now + str(lines) + '\n')
else:
with open('controlling.log.txt', 'w') as output_file:
output_file.write('start2 ' + Variables.time_now + ' Bad_status_code!' + '\n')
print('start2 ' + str(Variables.time_now) + ' Bad response, status_code= ' + str(
Variables.status_code_server_connections))
sleep(20.0)
except:
sleep(20.0)
continue
pass
sleep(20.0)
def check_Server_sensor_conections():
while True:
try:
rg = requests.get("http://f0555107.xsph.ru/hello.html") # резервный ('http://httpbin.org/get')
Variables.counting_requaest += 1
print('check server ' + str(rg.status_code))
if int(rg.status_code) == 200:
Variables.state_server_connections = 'Ok'
else:
Variables.state_server_connections = 'Error!'
Variables.status_code_server_connections = rg.status_code
rg.close()
del rg
sleep(20.0)
except:
print('except! Server')
Variables.state_server_connections = 'Some error!'
sleep(20.0)
continue
pass
sleep(20.0)
def check_req():
while True:
try:
r = requests.get('https://google.com/') # резервный ('http://httpbin.org/get')
print('check_reg ' + str(r.status_code))
if r.status_code == 200:
Variables.state_internet_connections = 'Ok'
print('check_reg')
else:
Variables.state_internet_connections = 'Error!'
print('check_reg')
Variables.status_code_check_req = r.status_code
gc.collect()
sleep(20.0)
# root.after(3000, check_req)
except:
print('except! Internet')
Variables.state_internet_connections = 'some error!'
sleep(20.0)
continue
pass
gc.collect()
sleep(20.0)
Иceive_from_server3 = 0
receive_from_server4 = 0
receive_from_server5 = 0
receive_from_server6 = 0
receive_from_server7 = 'home'
parsing_GPIO_4relay1 = [0, 0, 0, 0]
parsing_ESP1 = 0
Sadok_Light1 = 0
скрипт Variables:
from datetime import datetime
state_server_connections = ''
state_internet_connections = ''
state_exchange_data = 0
pomp_state = 2
status_code_server_connections = 0
counting_requaest = 0
time_now = datetime.now().strftime("%H:%M:%S")
receive_from_server = []
receive_from_server1 = 0
receive_from_server2 = 0
receive_from_server3 = 0
receive_from_server4 = 0
receive_from_server5 = 0
receive_from_server6 = 0
receive_from_server7 = 'home'
parsing_GPIO_4relay1 = [0, 0, 0, 0]
parsing_ESP1 = 0
Sadok_Light1 = 0
def change_res():
if receive_from_server7 == 'home':
a = 'Auto'
elif receive_from_server7 == 'remote':
a = 'remote'
else:
a = 'Ubnormal'
return a
change_recive = change_res()
Ответы (1 шт):
Автор решения: Александр Рассказчиков
→ Ссылка
Скорее всего, проблема с Threading
Данный модуль выполняет функции питона псевдоним-парралельно.https://hackernoon.com/concurrent-programming-in-python-is-not-what-you-think-it-is-b6439c3f3e6a
Вероятно, отрисовка окна Kivy и работа остального кода с while-ами является тяжёлой задачей для питона, из-за чего возникают проблемы с морганием.
Для оконных приложений лучше использовать более низкоуровневые языки программирования.