首页 > 解决方案 > Problem with Python not opening up files in the correct order

问题描述

I'm using Python 3.9 and Kivy

 from kivy.config import Config 
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width',  800)
Config.set('graphics', 'height', 600)
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.lang import Builder
import subprocess





presentation = Builder.load_file('UI_1.kv')

#Calling files to open 
#Always use popen 

class presentation(Widget):
    maths = ObjectProperty(None)
    physics = ObjectProperty(None)


    def btn_touch_up(maths):        
        from subprocess import Popen, PIPE
        subprocess.Popen(['python',  'UI_MATH.py'])
        exit()



    def btn_touch_up(physics):        
        from subprocess import Popen, PIPE
        subprocess.Popen(['python',  'UI_PHYSICS.py'])
        exit()




        

class MyApp(App): 
    def build(self):
        return presentation()





Window.clearcolor = (1, 1, 1, 1)




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

So that's the code I wrote. The problem is that when I run the app and click the first box (labeled maths), the code for the 2 box opens up (physics). When I clock the second box (Physics) it runs the correct code. Can someone point out my error so I can correct it? I'm fairly new so my code might seem like a mess...

Here is the code from the kivy (.kv) file:

<presentation>

    maths : maths
    physics : physics
    extra : extra


    canvas:
        Color:
            rgb:0,0,0
        Rectangle:
            pos:8,498
            size:790,50
        
        Rectangle:
            pos:8,398
            size:790,50
        
        Rectangle:
            pos:8,298
            size:790,50

    GridLayout:

    

        cols:1
        size: root.width -10, root.height -550
        pos: 5, 500


        GridLayout:
            cols:1      
    

            Button:
                id: maths
                text:"1.Maths"
                on_press: root.btn_touch_up()
                



    
    GridLayout:
        cols:1
        size: root.width -10, root.height -550
        pos: 5, 400


        GridLayout:
            cols:1          

            Button:
                id: physics
                text:"2.Physics"
                on_press: root.btn_touch_up()



    GridLayout:
        cols:1
        size: root.width -10, root.height -550
        pos: 5, 300


        GridLayout:
            cols:1


            

            Button:
                id: extra
                text:"3.Extra"
                on_press: root.btn_touch_up()
            

Thank you, and again, i'm really new and self taught so if there is something obviously wrong that I shouldn't do please point it out.

标签: pythonbuttonkivy

解决方案


您的presentation类代码对该方法有两个定义btn_touch_up()。结果是第二个定义覆盖了第一个定义,而第一个定义永远不能被调用(因为它不再存在)。您只需要创建唯一的方法名称,一个用于数学,另一个用于物理。


推荐阅读