我正在尝试使用kivy和python开发图灵机模拟器,我得到了基本的GUI设置。但是,当我单击顶部栏和侧栏中的按钮时,on_touch_down()函数会使on_press()函数重载。我该如何解决这个问题? 所以我想要的是当我点击舞台区域的任何地方时,调用on_touch_down()函数并打印坐标,当我点击任何按钮时,调用on_press()函数并且不打印任何内容。 这是我的代码:
GUIDisplay.py
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import *
class RootWidget(FloatLayout):
def add_transition_toggle(self):
print 'something happened'
def change_tape(self):
pass
def backgroundpress(self):
print 'region'
def actionbuttonpress(self):
print 'menu'
def on_touch_down(self, touch):
print touch.x, touch.y
class GUIDisplay(App):
def build(self):
app = RootWidget()
return app
def start_app():
GUIDisplay().run()
if '__main__' == __name__:
start_app()
GUIDisplay.kv
<RootWidget>:
# menu bar
id: toolbars
ActionBar:
pos_hint: {'top':1}
ActionView:
use_separator: True
ActionPrevious:
text: ''
title: 'Turing App'
with_previous: False
ActionGroup:
text: 'File'
mode: 'spinner'
size_hint_x: None
width: 90
ActionButton:
text: 'New'
on_press: root.actionbuttonpress()
ActionButton:
text: 'Open'
ActionButton:
text: 'Save'
ActionButton:
text: 'Save As'
ActionButton:
text: 'Quit'
ActionGroup:
text: 'Play'
mode: 'spinner'
size_hint_x: None
width: 90
ActionButton:
text: 'Play to End'
# tool bar
BoxLayout:
BoxLayout:
y: 10.0
x: 10.0
height: 400.0
width: 80.0
size_hint_y: 0.9
size_hint_x: 0.08
orientation: 'vertical'
opacity: 0.5
Button:
text: 'Select'
Button:
text: 'Add State'
Button:
text: 'Modify State'
Button:
on_release: root.add_transition_toggle()
on_press: pass
text: 'Add Transition'
Button:
text: 'Tape'
on_release: root.change_tape()
Label:
text: 'Stage Area'
答案 0 :(得分:0)
我是第一个承认这是丑陋的人,所以也许其他人可以找到更好的方法。我理解的问题是RelativeLayout正在消化触摸事件,因此不会在窗口小部件树上传递它。因此,您可以尝试沿着这些线检查孩子是否消化过它(按下按钮),如果没有,则打印x,y坐标。
def on_touch_down(self, touch):
event_handled = False
for child in self.children:
if not event_handled:
if child.dispatch('on_touch_down', touch):
event_handled = True
if not event_handled:
print touch.x, touch.y
希望这有助于......