Привет! Я хотел сделать приложение у которого есть меню и при переключение разделов открывался новый функционал. Например раздел “Калькулятор” и “Калькулятор квадратных уравнений”, Но я не могу встроить программу в интерфейс и ещё почему-то не настраивается размер окна(((. Я работаю с Kivy и Python всего лишь 4 дня, так что не кидайте тапками если ошибка будет очень глупая.
Вот код:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.recycleview import RecycleView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.config import ConfigParser
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.metrics import dp
import os
from kivy.config import Config
Config.set( "graphics", "resizable", 0 )
Config.set( "graphics", "width", 400 )
Config.set( "graphics", "height", 500 )
class MenuScreen(Screen):
def __init__(self, **kw):
super(MenuScreen, self).__init__(**kw)
box = BoxLayout(orientation='vertical')
box.add_widget(Button(text='Калькулятор', on_press=lambda x:
set_screen('list')))
box.add_widget(Button(text='Задачи',
on_press=lambda x: set_screen('add')))
self.add_widget(box)
class List(Screen):
def __init__(self, **kw):
super(List, self).__init__(**kw)
def update_label(self):
self.lbl.text = self.formula
def add_number(self, instance):
if( self.formula == "0" ):
self.formula = ""
self.formula += str(instance.text)
self.update_label()
def add_operation(self, instance):
if( str(instance.text).lower() == "x" ):
self.formula += "*"
else:
self.formula += str(instance.text)
self.update_label()
def calc_result(self, instance):
self.lbl.text = str(eval(self.lbl.text))
self.formula = "0"
gl = GridLayout(cols = 4, spacing=3, size_hint=(1, .6))
bl = BoxLayout(orientation = "vertical", padding=25)
gl.bind(minimum_height=gl.setter('height'))
self.formula = "0"
self.lbl = Label(text="0", font_size = 40, halign="right", valign = "center", size_hint =(1, .4), text_size=(400 - 50, 500 * .4 - 50))
bl.add_widget( self.lbl )
gl.add_widget( Button(text="7", on_press = self.add_number) )
gl.add_widget( Button(text="8", on_press = self.add_number) )
gl.add_widget( Button(text="9", on_press = self.add_number) )
gl.add_widget( Button(text="x", on_press = self.add_operation) )
gl.add_widget( Button(text="4", on_press = self.add_number) )
gl.add_widget( Button(text="5", on_press = self.add_number) )
gl.add_widget( Button(text="6", on_press = self.add_number) )
gl.add_widget( Button(text="-", on_press = self.add_operation) )
gl.add_widget( Button(text="1", on_press = self.add_number) )
gl.add_widget( Button(text="2", on_press = self.add_number) )
gl.add_widget( Button(text="3", on_press = self.add_number) )
gl.add_widget( Button(text="+", on_press = self.add_operation) )
gl.add_widget( Widget() )
gl.add_widget( Button(text="0", on_press = self.add_number) )
gl.add_widget( Button(text=".", on_press = self.add_number) )
gl.add_widget( Button(text="=", on_press = self.calc_result) )
bl.add_widget( gl )
return bl
def on_enter(self):
self.layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
self.layout.bind(minimum_height=self.layout.setter('height'))
back_button = Button(text='< Назад в главное меню',
on_press=lambda x: set_screen('menu'),
size_hint_y=None, height=dp(40))
self.layout.add_widget(back_button)
root = RecycleView(size_hint=(1, None), size=(Window.width,
Window.height))
root.add_widget(self.layout)
self.add_widget(root)
def on_leave(self): # Будет вызвана в момент закрытия экрана
self.layout.clear_widgets() # очищаем список
class Add(Screen):
def __init__(self, **kw):
super(Add, self).__init__(**kw)
self.layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
self.layout.bind(minimum_height=self.layout.setter('height'))
back_button = Button(text='< Назад в главное меню',
on_press=lambda x: set_screen('menu'),
size_hint_y=None, height=dp(40))
self.layout.add_widget(back_button)
root = RecycleView(size_hint=(1, None), size=(Window.width,
Window.height))
root.add_widget(self.layout)
self.add_widget(root)
def on_enter(self):
self.layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
self.layout.bind(minimum_height=self.layout.setter('height'))
back_button = Button(text='< Назад в главное меню',
on_press=lambda x: set_screen('menu'),
size_hint_y=None, height=dp(40))
self.layout.add_widget(back_button)
root = RecycleView(size_hint=(1, None), size=(Window.width,
Window.height))
root.add_widget(self.layout)
self.add_widget(root)
def on_leave(self): # Будет вызвана в момент закрытия экрана
self.layout.clear_widgets() # очищаем список
def set_screen(name_screen):
sm.current = name_screen
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(List(name='list'))
sm.add_widget(Add(name='add'))
class OptionsApp(App):
def __init__(self, **kvargs):
super(OptionsApp, self).__init__(**kvargs)
self.config = ConfigParser()
def build(self):
return sm
if __name__ == '__main__':
OptionsApp().run()