python - 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.
解决方案
您的presentation
类代码对该方法有两个定义btn_touch_up()
。结果是第二个定义覆盖了第一个定义,而第一个定义永远不能被调用(因为它不再存在)。您只需要创建唯一的方法名称,一个用于数学,另一个用于物理。
推荐阅读
- android - Play 游戏应用中未显示成就
- django - 如何使用 django 分页器页面范围来显示前 10 个数字而不是全部?
- java - 如何查看 Gradle 从哪个 URL 获得依赖项?
- html - Html 5 - 背景视频仅适用于没有静音标签的 chrome。
- python - Python3如何安装.ttf字体文件?
- html - 水平时间线 css 实现
- angularjs - AngularJS 意外路由
- spring - Spring托管与非托管bean
- javascript - HTML表格中的重叠行
- python - Python for Robotics:如何生成用于 Pepper 机器人本地化的应用程序