Python未按正确顺序打开文件的问题



我使用的是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()

谢谢你,再说一遍,我真的是个新手,自学成才,所以如果有什么明显的错误我不应该做,请指出。

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

最新更新