【问题标题】:Problem with Python not opening up files in the correct orderPython没有以正确的顺序打开文件的问题
【发布时间】:2021-01-03 12:42:18
【问题描述】:

我正在使用 Python 3.9 和 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()

这就是我写的代码。问题是当我运行应用程序并单击第一个框(标记为数学)时,打开 2 框的代码(物理)。当我为第二个盒子(物理)计时时,它运行正确的代码。有人可以指出我的错误以便我纠正吗?我还很新,所以我的代码可能看起来很乱......

这是来自 kivy (.kv) 文件的代码:

<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()
            

再次感谢您,我真的是新手,而且是自学成才的,所以如果有明显不应该做的错误,请指出。

【问题讨论】:

    标签: python button kivy


    【解决方案1】:

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

    【讨论】:

    • 你能给我一个唯一方法名的例子吗,不知道怎么做...谢谢!
    • 您可以使用btn_touch_up_math 数学和btn_touch_up_physics 物理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    相关资源
    最近更新 更多