@Dime38
Учу Python

Как из одного класса, вызвать переменную другого класса?

Привет, у меня есть код:
import pyowm
from pyowm.owm import OWM
from pyowm.utils.config import get_config_from

from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

from kivymd.uix.button import MDIconButton
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase


config_dict = get_config_from('config.json')
owm = pyowm.OWM('ed3d1e950f0d1e73b26cfd7f55fbb4f9', config_dict)

mgr = owm.weather_manager()

kv = '''
BoxLayout:
    orientation: "vertical"

    MDToolbar:
        title: "View Allthings"
        MDIconButton:
	    	icon: "youtube"
   		 theme_text_color: "Custom" 
    		md_text_color: 2, 9, 6, 1
    		icon_size: 1.2, 1.9
    		on_press: app.on_start()
	    
    MDTabs:
        id: tab
    

<w>:
	FloatLayout:
	    id: fl	
	    MDTextField:
	    	hint_text: "Название города"
	    	text: ""
	    	mode: "rectangle"
	    	id: ccity
	    	pos_hint: {'y': .90, 'x': .3}
	    	size_hint: .4, .067
		MDIconButton:
	    	icon: "check-bold"
	    	pos_hint: {'y': .89, 'x': .75}
	    	on_press: root.update()
	    	
	    	
	    	
	    	
	MDGridLayout:
		rows: 4
		size_hint_y: .8
		spacing: 200
		padding: 30
		pos_hint: {'y': .03, 'x': .05}
		MDIcon:
			icon: "thermometer-low"
		MDIcon:
			icon: "check-bold"
		MDIcon:
			icon: "twitter"
	       MDIcon:
			icon: "discord"
	
	MDGridLayout:
		cols: 2
	    rows: 4
		spacing: 100
		size_hint: .9, .8
		md_bg_color: 0, 0, 0, .2
		pos_hint: {'y': .03, 'x': .05}
	    MDLabel:
	    	id: label
	        text: "         Температура сейчас"
	        halign: "center"
	    MDLabel:
	    	id: res
	    	text: "None"
	        halign: "center"
	    MDLabel:
	        id: label2
	        text: "Min/max "
	        halign: "center"
	    MDLabel:
	        id: res4
	        text: "None"
	        halign: "center"
	    MDLabel:
	        id: label4
	        text: "Tab"
	        halign: "center"
	    MDLabel:
	        id: res1
	        text: "None"
	        halign: "center"
	    MDLabel:
	        id: label6
	        text: "Tab"
	        halign: "center"
	    MDLabel:
	        id: res2
	        halign: "center"
'''
    
class w(FloatLayout, MDTabsBase):
	def update(self):
	    if (self.ids.ccity.text == ""):
	    	city = "Ню Йорк"
	    else:
		    city = str(self.ids.ccity.text)
	    observation = mgr.weather_at_place(city)
	    w = observation.weather
	    l = w.temperature('celsius')['temp']
	    temp = str(l) + " °C"
	    self.ids.res.text = str(temp)

class k(FloatLayout, MDTabsBase):
	pass

ws = w()

class Main(MDApp):
    n = 3
    def build(self):
        return Builder.load_string(kv)
    
    def on_start(self):
    	file = open('city.txt', 'r', encoding='utf-8')
    	nms = ["Курс биткоина", "Курс доллара и эвро", "Коронавирус", "Погода"]
    	w1 = self.root.ids.tab.add_widget(w(text=nms[self.n]))
    	self.n -= 1
    	w2 = self.root.ids.tab.add_widget(k(text=nms[self.n]))
    	txtc = str(file.read())
    	pass
    	

if __name__ == '__main__':
	Main().run()


Мне нужно чтобы в TextField(он в классе "w"), при старте, задавалась переменная из класса Main(txtc)
На file.load не обращайте внимания
  • Вопрос задан
  • 310 просмотров
Решения вопроса 1
@radvsem
Main(txtc). В данном коде никак. Переменная txtc и классу Main то не принадлежит. Вам нужно или сохранить переменную в глобальной области видимости. Или сделать txtc переменной класса Main и вызывать через объект Main.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы