完全不明白除了监听事件之外如何实现获取鼠标位置,但是在事件队列为空的情况下,这是如何实现的呢?
pygamers的 pysdl 文档建议使用sdl2.mouse.SDL_GetMouseState() (doc here),但此函数实际上需要您想要询问的光标的 x,y 坐标。同时,调用sdl2.mouse.SDL_GetCursor()返回一个光标对象,但我找不到方法从中获取它的坐标(即它只是包装一个 C 对象,因此它有一个空.__dict__属性)。
我一直在尝试我能想到的一切,但我以前从未用C 编程过。我试图生成的简单包装函数只是:
def mouse_pos(self):
# ideally, just return <some.path.to.mouse_x_y>
event_queue = sdl2.SDL_PumpEvents()
state = sdl2.mouse.SDL_GetMouseState(None, None) # just returns 0, so I tried the next few lines
print state
for event in event_queue:
if event.type == sdl2.SDL_MOUSEMOTION:
# this works, except if the mouse hasn't moved yet, in which case it's none
return [event.x, event.y]
Run Code Online (Sandbox Code Playgroud) 我正在尝试建立一个包含很多hasMany/hasOne关系的模型,我希望在"添加"视图的下拉菜单中按字母顺序显示所拥有的模型.我不确定如何向递归检索的模型提供ORER BY指令.这就是我尝试过的:
$services = $this->service->find('all', array(
'order'=>array(
'Service.start_year DESC',
'Servicecat.title DESC'
),
'recursive'=>0
) );
Run Code Online (Sandbox Code Playgroud)
但当然这没有变化.我怀疑它不是很复杂,我只是在cookbook/api中找不到解决方案.想法?
model-view-controller sql-order-by recursive-query cakephp-2.0
这可能是一个超级问题,道歉.我正在尝试在PHP中实现一个效果我熟悉PHP,它正在为私有属性构建getter和setter函数.
在PHP中,我通常会这样做:
<?php
class newClass() {
private $var = null;
function newClass($initVal) {
//init
}
public function get($var) {
if ( isset($this->$var) ) {
return $this->$var;
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
但是,在Python中尝试相同的想法:
class newClass():
def __init__(self, initVal):
self.__nameOfVar1 = initVal
self.__nameOfVar2 = initVal
def get(self, var):
#assuming, when called, var = nameOfVar1
if self.__var:
return self.__var
Run Code Online (Sandbox Code Playgroud)
引发错误 AttributeError:'newClass' object has no attribute '__var'
而且非常正确,事实并非如此.如何构建getter和setter函数来访问私有属性?
更新:
我已经离开了原来的问题,因为对于Python的新成员来说,了解他们可能会意外提出的其他语言的惯例是有用的.但实际上,我最初的问题有点不同,在尝试使问题更加普遍时,我模糊了我的实际需要.所以这是一个修正案:
事实上,我没有使用私有属性; 事实上,我正在尝试通过组合环境向下传递属性.即:
Class A,作为属性,具有Class B不从中继承的对象Class A.几乎从来没有Class B需要任何信息的情况Class …
python新手,如果这是nub东西,请道歉:
写一个小模块(可能是一个包,最终?).它对于单个文件来说太大了,所以我将我的大型类移动到单个文件中.我的所有类都扩展了一个我用来保存一些基本功能的基类.
这是我的问题的一般形式:
runtime.py:
import baseclass
import subclass
#do stuff
Run Code Online (Sandbox Code Playgroud)
baseclass.py
class BaseClass():
def __init__(self):
pass
def sharedMethod(self):
pass
# shared functionality
Run Code Online (Sandbox Code Playgroud)
subclass.py
class SubClass(baseclass.BaseClass):
def __init__(self):
BaseClass.__init__(self)
Run Code Online (Sandbox Code Playgroud)
追溯记录如下:
Traceback (most recent call last):
File "/path/to/runtime.py", line 2, in <module>
import baseclass, subclass
File "path/to/subclass.py", line 2, in <module>
class Subclass(baseclass.BassClass):
NameError: name 'baseclass' is not defined
Run Code Online (Sandbox Code Playgroud)
如果我导入baseclass到subclass.py它工作正常,但是这似乎意味着,每一个延伸类baseclass需要导入它,它musn't是适当的方式做,这是进口的情况下,baseclass一遍又一遍.
我希望能够按照它们相互构建的顺序从单独的文件中导入类,我只是不知道该怎么做.建议吗?
注意:所有答案都有帮助/正确.对于"正确的",我只选择了最彻底/最有可能对每个人都有用的答案.干杯,谢谢.:)
遵循这个混合两个颜色值的 alpha 公式,我希望将其应用于rgba 图像数据的n 个numpy 数组(尽管预期的用例在实践中将具有非常低的数组上限,可能 > 5)。在上下文中,这个过程将被限制为相同形状的数组。
理论上我可以通过迭代来实现这一点,但预计这将是计算密集型且效率极低的。
在整个数组的两个数组之间相同位置的两个元素之间应用函数的最有效方法是什么?
一个松散的例子:
# in context, the numpy arrays come from here, as either numpy data in the
# first place or a path
def import_data(source):
# first test for an extant numpy array
try:
assert(type(source) is np.ndarray)
data = source
except AssertionError:
try:
exists(source)
data = add_alpha_channel(np.array(Image.open(source)))
except IOError:
raise IOError("Cannot identify image data in file '{0}'".format(source))
except TypeError:
raise TypeError("Cannot identify image data from source.") …Run Code Online (Sandbox Code Playgroud) python ×4
oop ×2
arrays ×1
cakephp-2.0 ×1
getter ×1
inheritance ×1
numpy ×1
php ×1
pysdl2 ×1
python-2.7 ×1
sdl ×1
sql-order-by ×1