Reputation: 11
I am trying to run my function in a thread so that GUI doesn't freeze. However, it isn't working. I tried to thread the GUI call in the "main". I also tried to call to thread the function which is taking time for calculation. None of of the methods seems working. What am I doing wrong?
My .py file
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
import os
import pathlib
import main
import threading
class FirstWindow(Screen):
def select_cities(self, country):
cities = []
for file in os.listdir(r'WeatherFiles/'+country):
# check the files which are end with specific extension
if file.endswith(".txt"):
y = pathlib.Path(file)
cities.append(y.stem)
self.ids.city_list.values = cities
def selected_city(self, city):
self.ids.output.text = "[b]Please upload the weather file![/b]"
def getdata(self, city,country):
global data_file
data_file = 'WeatherFiles/'+country+'/'+city+'.txt'
self.ids.output.text = "Please Wait..."
x = threading.Thread(target=self.uploaddata(data_file))
x.start()
def uploaddata(self, data_file):
self.ids.output.text = 'Please wait...'
try:
main.ClimateData(data_file)
self.ids.output.text = '[b]Weather Data Uploaded Successfully![/b]'
except:
self.ids.output.text = 'Sorry! There was some error!'
class SecondWindow(Screen):
def exitf(self):
os._exit(0)
class ThirdWindow(Screen):
def inputs(self):
y = threading.Thread(target=self.calculate())
y.start()
def calculate(self):
Electricity_rate = float(self.manager.get_screen("second").ids.Electricity_rate.text)
CO2_rate = float(self.manager.get_screen("second").ids.CO2_rate.text)
Set_temp = float(self.manager.get_screen("second").ids.Set_temp.text)
New_set_temp = float(self.manager.get_screen("second").ids.New_set_temp.text)
Wall_thickness = float(self.manager.get_screen("second").ids.Wall_thickness.text)
Roof_thickness = float(self.manager.get_screen("second").ids.Roof_thickness.text)
Thermal_conductivity_wall = float(self.manager.get_screen("second").ids.Thermal_conductivity_wall.text)
Thermal_conductivity_roof = float(self.manager.get_screen("second").ids.Thermal_conductivity_roof.text)
Wall_area = float(self.manager.get_screen("second").ids.Wall_area.text)
Roof_area = float(self.manager.get_screen("second").ids.Roof_area.text)
COP_chillers = float(self.manager.get_screen("second").ids.COP_chillers.text)
try:
main.Calculation(data_file, Electricity_rate, CO2_rate, Set_temp, New_set_temp, Wall_thickness,
Thermal_conductivity_wall, Roof_thickness, Thermal_conductivity_roof, Roof_area, Wall_area,
COP_chillers)
self.ids.output.text = 'Calculation Successful!'
Annual_saving, Annual_saving_per, Annual_saving_SAR, Annual_saving_CO2 = main.Calculation(data_file,
Electricity_rate,
CO2_rate,
Set_temp,
New_set_temp,
Wall_thickness,
Thermal_conductivity_wall,
Roof_thickness,
Thermal_conductivity_roof,
Roof_area,
Wall_area,
COP_chillers)
self.ids.Annual_saving.text = str(Annual_saving)
self.ids.Annual_saving_per.text = str(Annual_saving_per)
self.ids.Annual_saving_SAR.text = str(Annual_saving_SAR)
self.ids.Annual_saving_CO2.text = str(Annual_saving_CO2)
except Exception as e:
# self.ids.output.text = str(e)
self.ids.output.text = 'Weather Data not Found!'
def select_countries():
countries = []
for file in os.listdir(r'WeatherFiles/'):
y = pathlib.Path(file)
countries.append(y.stem)
return countries
class EnergyApp_v_22_11_02(App):
select_countries()
countries = select_countries()
def build(self):
self.root_layout = ScreenManager()
return self.root_layout
if __name__ == "__main__":
threading.Thread(target=EnergyApp_v_22_11_02().run())
Here is my .kv file:
#:import get_color_from_hex kivy.utils.get_color_from_hex
#:import Gradient kivy_gradient.Gradient
#:import threading threading
<ScreenManager>:
FirstWindow:
name: "first"
SecondWindow:
name: "second"
ThirdWindow:
name: "third"
<FirstWindow>:
canvas.before:
#Color:
# rgba: (77/255,68/255,101/255,1)
Rectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#d9e6f2"), get_color_from_hex("#f7fafc"))
FloatLayout:
pos: 0, 0
size: root.width, root.height
Image:
source: 'Resources/Logo.png'
pos_hint: {'x': 0, 'y': .88 }
size_hint: (.1, .1)
Label:
id: output
canvas.before:
Color:
rgba: (245/255, 245/255,245/255,1)
RoundedRectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#f7baba"), get_color_from_hex("#f7baba"))
radius: [10]
background_normal: ''
background_color: (0,0,0,0)
markup: True
text: '[i][b]Select the Location[/b][/i]'
font_size: 18
color: '#FFFFFF'
padding_x: 25
padding_y: 25
pos_hint: {'x': .1, 'y': .4 }
size_hint: (.5, .4)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Spinner:
id: country_list
canvas.before:
Color:
rgba: (245/255, 245/255,245/255,1)
RoundedRectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#97b5ad"), get_color_from_hex("#97b5ad"))
radius: [5]
background_normal: ''
background_color: (0,0,0,0)
markup:True
text: '[b]Countries[/b]'
font_size: 18
values: app.countries
pos_hint: {'x': .65, 'y': .71 }
size_hint: (.2, .08)
on_text: root.select_cities(self.text)
Spinner:
id: city_list
canvas.before:
Color:
rgba: (245/255, 245/255,245/255,1)
RoundedRectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#97b5ad"), get_color_from_hex("#97b5ad"))
radius: [5]
background_normal: ''
background_color: (0,0,0,0)
markup:True
text: '[b]Cities[/b]'
font_size: 18
#values: root.cities
pos_hint: {'x': .65, 'y': .6 }
size_hint: (.2, .08)
on_text: root.selected_city(self.text)
Button:
id: upload_data
canvas.before:
Color:
rgba: (245/255, 245/255,245/255,1)
RoundedRectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#2e5c84"), get_color_from_hex("#2e5c84"))
radius: [2]
background_normal: ''
background_color: (0,0,0,0)
markup:True
text: 'Upload'
font_size: 18
#background_normal: ''
#background_color: (0,0,0,0)
pos_hint: {'x': .65, 'y': .4 }
size_hint: (.2, .08)
on_press:
output.text: '[i]Please Wait...[/i]'
on_release: threading.Thread(target=root.getdata(city_list.text, country_list.text)).start()
Button:
canvas.before:
Color:
rgba: (245/255, 245/255,245/255,1)
RoundedRectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#21425e"), get_color_from_hex("#21425e"))
radius: [2]
background_normal: ''
background_color: (0,0,0,0)
text: "Next"
font_size: 17
pos_hint: {'x': .75, 'y': .08 }
size_hint: (.17, .07)
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
canvas.before:
#Color:
# rgba: (77/255,68/255,101/255,1)
Rectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#f9fbfb"), get_color_from_hex("#FFFFFF"))
FloatLayout:
pos: 0, 0
size: root.width, root.height
Image:
source: 'Resources/Logo.png'
pos_hint: {'x': 0, 'y': .88 }
size_hint: (.1, .1)
Label:
id: label_1
markup: True
text: 'Electricity Rate'
font_size: 17
color: '#21425f'
padding_y: 8
pos_hint: {'x': .07, 'y': .8 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Electricity_rate
font_size: 17
text: '0.25'
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .8 }
size_hint: (.09, .06)
Label:
id: label_2
markup: True
text: '[i]SAR/kWh[/i]'
font_size: 17
color: '#21425f'
padding_y: 8
pos_hint: {'x': .38, 'y': .8 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_3
markup: True
text: '[i]CO2 Rate[/i]'
font_size: 15
color: '#161748'
padding_y: 8
pos_hint: {'x': .49, 'y': .8 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: CO2_rate
font_size: 17
text: '0.569'
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .7, 'y': .8 }
size_hint: (.09, .06)
Label:
id: label_4
markup: True
text: '[i]kg/kWh[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .8, 'y': .8 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_5
markup: True
text: '[i]Set-point Temperature[/i]'
font_size: 16
color: '#161748'
padding_y: 8
multiline: True
pos_hint: {'x': .07, 'y': .7 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Set_temp
text: '21.0'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .7 }
size_hint: (.09, .06)
Label:
id: label_6
markup: True
text: '[i]C[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .38, 'y': .7 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_7
markup: True
text: '[i]New Set-point Temperature[/i]'
font_size: 15
color: '#161748'
#padding_y: 8
multiline: True
pos_hint: {'x': .49, 'y': .7 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: New_set_temp
text: '23.5'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .7, 'y': .7 }
size_hint: (.09, .06)
Label:
id: label_8
markup: True
text: '[i]C[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .8, 'y': .7 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_9
markup: True
text: '[i]Wall Thickness[/i]'
font_size: 16
color: '#161748'
padding_y: 8
multiline: True
pos_hint: {'x': .07, 'y': .6 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Wall_thickness
text: '20.0'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .6 }
size_hint: (.09, .06)
Label:
id: label_10
markup: True
text: '[i]cm[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .38, 'y': .6 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_11
markup: True
text: '[i]Roof Thickness[/i]'
font_size: 15
color: '#161748'
padding_y: 8
#multiline: True
pos_hint: {'x': .49, 'y': .6 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Roof_thickness
text: '20.0'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .7, 'y': .6 }
size_hint: (.09, .06)
Label:
id: label_12
markup: True
text: '[i]cm[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .8, 'y': .6 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_13
markup: True
text: '[i]Wall Thermal Conductivity[/i]'
font_size: 15
color: '#161748'
#padding_y: 8
multiline: True
pos_hint: {'x': .07, 'y': .5 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Thermal_conductivity_wall
text: '1.2'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .5 }
size_hint: (.09, .06)
Label:
id: label_14
markup: True
text: '[i]W/mC[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .38, 'y': .5 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_15
markup: True
text: '[i]Roof Thermal Conductivity[/i]'
font_size: 15
color: '#161748'
#padding_y: 8
multiline: True
pos_hint: {'x': .49, 'y': .5 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Thermal_conductivity_roof
text: '1.2'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .7, 'y': .5 }
size_hint: (.09, .06)
Label:
id: label_16
markup: True
text: '[i]W/mC[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .8, 'y': .5 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_17
markup: True
text: '[i]Wall Area[/i]'
font_size: 15
color: '#161748'
padding_y: 8
#multiline: True
pos_hint: {'x': .07, 'y': .4 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Wall_area
text: '490.0'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .4 }
size_hint: (.09, .06)
Label:
id: label_18
markup: True
text: '[i]m2[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .38, 'y': .4 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_19
markup: True
text: '[i]Roof Area[/i]'
font_size: 15
color: '#161748'
padding_y: 8
#multiline: True
pos_hint: {'x': .49, 'y': .4 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: Roof_area
text: '688.0'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .7, 'y': .4 }
size_hint: (.09, .06)
Label:
id: label_20
markup: True
text: '[i]m2[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .8, 'y': .4 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_21
markup: True
text: '[i]COP of the Chiller[/i]'
font_size: 15
color: '#161748'
padding_y: 8
#multiline: True
pos_hint: {'x': .49, 'y': .3 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
TextInput:
id: COP_chillers
text: '3.5'
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .7, 'y': .3 }
size_hint: (.09, .06)
Label:
id: label_22
markup: True
text: '[i][/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .8, 'y': .3 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Button:
text: "Next"
font_size: 17
pos_hint: {'x': .7, 'y': .1 }
size_hint: (.2, .08)
on_release:
app.root.current = "third"
root.manager.transition.direction = "left"
Button:
text: "Back"
font_size: 17
pos_hint: {'x': .1, 'y': .1 }
size_hint: (.2, .08)
on_release:
app.root.current = "first"
root.manager.transition.direction = "right"
<ThirdWindow>:
canvas.before:
#Color:
# rgba: (77/255,68/255,101/255,1)
Rectangle:
pos: self.pos
size:self.size
texture: Gradient.vertical(get_color_from_hex("#e5e7e9"), get_color_from_hex("898AA6"))
FloatLayout:
pos: 0, 0
size: root.width, root.height
Label:
id: output
canvas.before:
Color:
rgba: (245/255, 245/255,245/255,1)
Rectangle:
pos: self.pos
size:self.size
texture: Gradient.horizontal(get_color_from_hex("C9BBCF"), get_color_from_hex("#EBEDEF"))
markup: True
text: '[i]Press Calculate[/i]'
font_size: 17
color: '#161748'
padding_x: 15
padding_y: 10
pos_hint: {'x': .25, 'y': .75 }
size_hint: (.5, .1)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Button:
id: calculate
markup:True
text: 'Calculate'
font_size: 18
#background_normal: ''
#background_color: (0,0,0,0)
pos_hint: {'x': .55, 'y': .65 }
size_hint: (.2, .08)
on_release: root.inputs()
Label:
id: label_23
markup: True
text: '[i]Annual Electricity Saving[/i]'
font_size: 15
color: '#161748'
#padding_y: 8
multiline: True
pos_hint: {'x': .07, 'y': .5 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
Label:
id: Annual_saving
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .5 }
size_hint: (.09, .06)
Label:
id: label_24
markup: True
text: '[i]kWh[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .38, 'y': .5 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_25
markup: True
text: '[i]Percentage Reduction[/i]'
font_size: 15
color: '#161748'
padding_y: 8
#multiline: True
pos_hint: {'x': .54, 'y': .5 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
Label:
id: Annual_saving_per
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .75, 'y': .5 }
size_hint: (.09, .06)
Label:
id: label_26
markup: True
text: '[i]%[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .85, 'y': .5 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_27
markup: True
text: '[i]Annual Bill Saving[/i]'
font_size: 15
color: '#161748'
#padding_y: 8
multiline: True
pos_hint: {'x': .07, 'y': .4 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
Label:
id: Annual_saving_SAR
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .28, 'y': .4 }
size_hint: (.09, .06)
Label:
id: label_28
markup: True
text: '[i]SAR[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .38, 'y': .4 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Label:
id: label_29
markup: True
text: '[i]CO2 Emission Reduction[/i]'
font_size: 15
color: '#161748'
padding_y: 8
#multiline: True
pos_hint: {'x': .54, 'y': .4 }
size_hint: (.2, .06)
text_size: self.width, self.height
halign: 'right'
valign: 'top'
Label:
id: Annual_saving_CO2
foreground_color: (22/255, 23/255,72/255,1)
#background_color: (245/255, 245/255,245/255,1)
multiline: False
padding_x: 15
padding_y: 10
pos_hint: {'x': .75, 'y': .4 }
size_hint: (.09, .06)
Label:
id: label_30
markup: True
text: '[i]tCO2[/i]'
font_size: 16
color: '#161748'
padding_y: 8
pos_hint: {'x': .85, 'y': .4 }
size_hint: (.09, .06)
text_size: self.width, self.height
halign: 'left'
valign: 'top'
Button:
text: "Exit"
font_size: 17
pos_hint: {'x': .7, 'y': .1 }
size_hint: (.2, .08)
on_release: root.exitf()
Button:
text: "Back"
font_size: 17
pos_hint: {'x': .1, 'y': .1 }
size_hint: (.2, .08)
on_release:
app.root.current = "second"
root.manager.transition.direction = "right"
Upvotes: 1
Views: 280
Reputation: 627
this sequence is the way to go:
x = threading.Thread(target=self.uploaddata, args=(datafile, ))
x.start()
EDIT: my apologies, I missed the syntax of the call to Thread previously. note the use of args
If the thread starts and ends relatively quickly or deterministically this is fine. You can also start a thread that runs forever and if you have that kind then you should set daemon=True when creating the Thread object and this will cause it to automatically terminate when your main application stops.
Python File
#
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
import os
import pathlib
import main
import threading
class FirstWindow(Screen):
def __init__(self, **kw):
super().__init__(**kw)
def select_cities(self, country):
cities = []
for file in os.listdir(r'WeatherFiles/'+country):
# check the files which are end with specific extension
if file.endswith(".txt"):
y = pathlib.Path(file)
cities.append(y.stem)
self.ids.city_list.values = cities
def selected_city(self, city):
self.ids.output.text = "[b]Please upload the weather file![/b]"
def getdata(self, city,country):
global data_file
data_file = str(pathlib.Path('WeatherFiles', country, city, ".txt"))
self.ids.output.text = "Please Wait..."
x = threading.Thread(target=self.uploaddata(data_file))
x.start()
def uploaddata(self, data_file):
self.ids.output.text = 'Please wait...'
try:
main.ClimateData(data_file)
self.ids.output.text = '[b]Weather Data Uploaded Successfully![/b]'
except:
self.ids.output.text = 'Sorry! There was some error!'
class SecondWindow(Screen):
def exitf(self):
os._exit(0)
class ThirdWindow(Screen):
def exitf(self):
print("exiting the app")
App.get_running_app().stop()
def inputs(self):
y = threading.Thread(target=self.calculate())
y.start()
def calculate(self):
Electricity_rate = float(self.manager.get_screen("second").ids.Electricity_rate.text)
CO2_rate = float(self.manager.get_screen("second").ids.CO2_rate.text)
Set_temp = float(self.manager.get_screen("second").ids.Set_temp.text)
New_set_temp = float(self.manager.get_screen("second").ids.New_set_temp.text)
Wall_thickness = float(self.manager.get_screen("second").ids.Wall_thickness.text)
Roof_thickness = float(self.manager.get_screen("second").ids.Roof_thickness.text)
Thermal_conductivity_wall = float(self.manager.get_screen("second").ids.Thermal_conductivity_wall.text)
Thermal_conductivity_roof = float(self.manager.get_screen("second").ids.Thermal_conductivity_roof.text)
Wall_area = float(self.manager.get_screen("second").ids.Wall_area.text)
Roof_area = float(self.manager.get_screen("second").ids.Roof_area.text)
COP_chillers = float(self.manager.get_screen("second").ids.COP_chillers.text)
try:
main.Calculation(data_file, Electricity_rate, CO2_rate, Set_temp, New_set_temp, Wall_thickness,
Thermal_conductivity_wall, Roof_thickness, Thermal_conductivity_roof, Roof_area, Wall_area,
COP_chillers)
self.ids.output.text = 'Calculation Successful!'
Annual_saving, Annual_saving_per, Annual_saving_SAR, Annual_saving_CO2 = main.Calculation(data_file,
Electricity_rate,
CO2_rate,
Set_temp,
New_set_temp,
Wall_thickness,
Thermal_conductivity_wall,
Roof_thickness,
Thermal_conductivity_roof,
Roof_area,
Wall_area,
COP_chillers)
self.ids.Annual_saving.text = str(Annual_saving)
self.ids.Annual_saving_per.text = str(Annual_saving_per)
self.ids.Annual_saving_SAR.text = str(Annual_saving_SAR)
self.ids.Annual_saving_CO2.text = str(Annual_saving_CO2)
except Exception as e:
# self.ids.output.text = str(e)
self.ids.output.text = 'Weather Data not Found!'
def select_countries():
countries = []
for file in os.listdir(r'WeatherFiles/'):
y = pathlib.Path(file)
countries.append(y.stem)
return countries
class MyScreenManager(ScreenManager):
FirstWindow: Screen
SecondWindow: Screen
ThirdWindow: Screen
def __init__(self, **kwargs):
super().__init__(**kwargs)
class EnergyApp(App):
select_countries()
countries = select_countries()
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self) -> MyScreenManager:
self.root_layout = MyScreenManager()
self.first_window = FirstWindow(name="first")
self.second_window = SecondWindow(name="second")
self.third_window = ThirdWindow(name="third")
self.root_layout.add_widget(self.second_window, )
self.root_layout.add_widget(self.third_window, )
self.root_layout.add_widget(self.first_window, )
return self.root_layout
def on_start(self):
pass
self.root_layout.current = 'first'
if __name__ == "__main__":
main_gui_app = EnergyApp()
# what is the name of the kv file
main_gui_app.kv_file = "EnergyApp_v_22_11_02.kv"
main_gui_app.run()
You don't want to start the main application as a Thread. I think the best is to call a regular function in the .kv file and then in your Python code you can start a function as a Thread.
It is more straightforward to explicitly declare the filename in the code. I added the line to set the property kv_file directly.
What is going wrong when you start a thread in the function call that is executed from a kivy widget? Maybe the problem isn't related to that technique but some other issue.
Upvotes: 0