python - 在两个 kivy 屏幕中添加两个值并得到结果

标签 python user-interface kivy screen

我正在尝试构建一个可以计算两个值之和的应用程序。我有一个名为 Therdwindow 的屏幕,它具有三个文本输入小部件。

from kivy.app import App 
from kivy.app import App 
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen`
class MainWindow(Screen):
    pass
class SecondWindow(Screen):
    pass
class Therdwindow(Screen):
    pass
class Fourwindow(Screen):
    pass
class FunfthWindow(Screen):
    def calculate3(self,bibi,mimi):
        kiki = str(float(bibi) + float(mimi))
        if kiki:
            try :
                self.result_1.text = str(eval(kiki)) 
            except Exception: 
                self.result_1.text = 'Error'

class Sixwindow(Screen):
    pass
class WindowManager(ScreenManager):
    psss
kv = Builder.load_file("layout.kv")
class MyMainApp(App):
    def build(self):
        return kv
if __name__ == "__main__":
    MyMainApp().run()

一个人应该输入密码“gin”,然后单击“NEXT”按钮转到名为 SecondWindow 的下一个屏幕,然后单击“NEXT”按钮转到名为 TherdWindow 的下一个屏幕,

然后在框中输入第一个值,然后单击“下一步”按钮转到名为 fourWindow 的下一个屏幕, 输入第二个值并单击“下一步”按钮转到名为 funfthWindow 的下一个屏幕。

如果他点击按钮结果,应该有“结果”。在此屏幕中,有一个标签,应打印该人指定的总和的体积。

布局.kv

<CustButton@Button>: 
     font_size: 40
WindowManager:
    MainWindow:
    SecondWindow:
    Therdwindow:
    Fourwindow:
    FunfthWindow:
    Sixwindow:

<MainWindow>:
     name: "main"

    <MainWindow>:
        name: "main"
        GridLayout:
            cols:1

            GridLayout:
                cols: 2
                orientation: 'vertical'

                Label:
                    text: "Password: "
                    font_size: "40sp"
                    background_color: (1,1,0,1)
                    font_name: 'RobotoMono-Regular.ttf'

                TextInput:
                    id: passw
                    multiline: False
                    font_size: "40sp"

            CustButton:
                text: "Submit"
                background_color: (0.8,0.8,0,1)
                on_press:
                    app.root.current = "second" if passw.text == "gin" else "six"
                    root.manager.transition.duration = 1 
                    root.manager.transition.direction = "left"


<SecondWindow>:
     name: "second"

     GridLayout:

         cols: 1
         spacing: 10
         CustButton:
             text: "Go Back"
             on_press:
                 app.root.current = "main"
                 root.manager.transition.direction = "right"

         CustButton:
             text: "next"
             on_press:
                 app.root.current = "therd"
                 root.manager.transition.direction = "left"


<Therdwindow>:
     id:lulu
     name: "therd"
     nani:feras1
     rows: 20
     padding: 0
     spacing: 2



     GridLayout:
         spacing: 10
         cols:2
         Label:
             text: "Enter The First Value :  "
             font_size: "30sp"


         TextInput: 
             id:first_Value
             font_size: 40
             multiline: True



         CustButton: 
             text: "go back"
             on_press:
                 app.root.current = "second"
                 root.manager.transition.direction = "right"

         CustButton: 
             text: "Next"
             on_press:
                 app.root.current = "four"
                 root.manager.transition.direction = "left"


<Fourwindow>:
     id:lala
     name: "four"
     nani21:feras2
     rows: 20
     padding: 0
     spacing: 2



     GridLayout:
         spacing: 10
         cols:2
         Label:
             text: "Enter The second Value : "
             font_size: "30sp"



         TextInput: 
             id: second_Value
             font_size: 40
             multiline: True



         CustButton: 
             text: "go back"
             on_press:
                 app.root.current = "therd"
                 root.manager.transition.direction = "right"

         CustButton: 
             text: "NEXT"
             on_press:
                 app.root.current = "funfth"
                 root.manager.transition.direction = "left"


<FunfthWindow>: 
     id:CalcmGridLayout
     name: "funfth"            
     result_1:label_id
     rows: 20
     padding: 0
     spacing: 2



     GridLayout:
         spacing: 10
         cols:2

         CustButton: 
             text: "Result :  "
             font_size: "30sp"
             on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)

         Label: 
             id: label_id
             font_size: 40
             multiline: True    

         CustButton: 
             text: "go back"
             on_press:
                 app.root.current = "four"
                 root.manager.transition.direction = "right"

         CustButton: 
             text: "NEXT"
             on_press:
                 app.root.current = "main"
                 root.manager.transition.direction = "left"


<Sixwindow>:
     name: "six"            
     GridLayout:
         cols: 1
         spacing: 10
         Label:
             text: 'das Password ist falsch'
             font_size: "40sp"

         CustButton:
             text: "nochmal"
             on_press:
                 app.root.current = "main"
                 root.manager.transition.direction = "right"

当我点击“结果”时,我收到此错误 NameError:first_Value 未定义

请帮忙。我真的很感激任何建议。

最佳答案

嗯,有很多方法可以做到这一点,为了简单起见,我将在 MyMainApp 类中执行所有操作:

 from kivy.properties import ObjectProperty
 from Kivy.clock import Clock
...
class Therdwindow(Screen):

    first_Value = ObjectProperty(None)

    def getvalue(self):
        return self.first_Value

class Fourwindow(Screen):

    second_Value = ObjectProperty(None)

    def getvalue(self):
        return self.second_Value

class FunfthWindow(Screen):

    label_id = ObjectProperty(None)

    def getlabel(self):
        return self.label_id

class WindowManager(ScreenManager):
    pass


class MyMainApp(App):
    def build(self):
        with open('layout.kv', encoding='utf-8', errors='ignore') as f:
            Builder.load_string(f.read())
        # creating objects of screens
        self.mainwindow = MainWindow()
        self.secondwindow = SecondWindow()
        self.therdwindow = Therdwindow()
        self.fourwindow = Fourwindow()
        self.funfthwindow = FunthWindow()

        # creating object of screen manager
        self.sm = WindowManager()

        # getting all object properties
        self.first_Value = self.therdwindow.getvalue()
        self.second_Value = self.fourwindow.getvalue()
        self.label_id = self.funfthwindow.getlabel()

        # connecting object properties to widgets from kv file
        # sometimes we need to delay that, because the app can't load so quickly, so let's make a method for it
        Clock.schedule_once(self.getids)

        # adding screens to screen manager
        self.sm.add_widget(self.mainwindow)
        self.sm.add_widget(self.secondwindow)
        self.sm.add_widget(self.therdwindow)
        self.sm.add_widget(self.fourwindow)
        self.sm.add_widget(self.funfthwindow)
        # in case you want screen manager be able to work you should return it
        return self.sm

    def getids(self):
        self.first_Value = self.therdwindow.ids.first_Value
        self.second_Value = self.fourwindow.ids.second_Value
        self.label_id = self.funfthwindow.ids.label_id

    # method that does what you want
    def count(self):
        self.label_id.text = str(int(self.first_Value.text) + int(self.second_Value.text))

if __name__ == "__main__":
    MyMainApp().run()

您需要编辑您的 kv 文件,更改所有内容

on_press:
    app.root.current = "second"

对此:

on_press:
    app.sm.current = "second"

并更改此:

CustButton: 
             text: "Result :  "
             font_size: "30sp"
             on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)

对此:

CustButton: 
             text: "Result :  "
             font_size: "30sp"
             on_press: app.count

您还需要在类中添加该行:

<Therdwindow>:
    ...
    first_Value: first_Value.__self__

<Fourwindow>:
    ...
    second_Value: second_Value.__self__

<FunfthWindow>:
    ...
    label_id: label_id.__self__

并删除此:

WindowManager:
    MainWindow:
    SecondWindow:
    Therdwindow:
    Fourwindow:
    FunfthWindow:
    Sixwindow:

关于python - 在两个 kivy 屏幕中添加两个值并得到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60007855/

相关文章:

c# - 用于桌面应用程序的 Java Swing 或 Windows 窗体?

android - 如何在 ListView 上使用 ScrollEffect 来防止过度滚动?

ubuntu - 用 buildozer 编译后如何删除无用的文件?

python - 如何禁用 Django 的 CSRF 验证?

python - 如何在python中水平翻转mp4视频?

macos - Cocoa:循环遍历窗口中的所有控件?

python - 使用 Builder.load_string 在 Kivy 应用程序上显示变量

python - 如何调试异步python代码?

python - 运行玻璃框架时 _BaseServer__is_shut_down

java - 有没有办法在java控制台中对扫雷游戏的行和列进行编号?