>>> import os
>>> os.chdir('c:/python27')
>>> os.listdir('c:')
['Tools', 'include', 'python.exe', 'libs', 'DLLs', 'Lib', 'NEWS.txt',
'w9xpopen.exe', 'Doc', 'pythonw.exe', 'LICENSE.txt', 'README.txt', 'tcl']
>>> os.listdir('c:/')
['users', 'Program Files', 'Python27', 'windows']
Run Code Online (Sandbox Code Playgroud)
为什么"c:"之后的"/"会影响结果?有没有办法os.listdir('c:')返回"c:/"的内容?
我使用Python作为我的例子,但我的问题一般是指编程语言.
def some_function(eggs):
if eggs == 1:
do_something_1()
elif eggs == 2:
do_something_2()
elif eggs == 3:
do_something_3()
else:
do_error()
return
do_something_4()
do_something_5()
do_something_6()
Run Code Online (Sandbox Code Playgroud)
(这只是一个例子.我的函数不会被调用do_something_x.)
像这样在其他方面回归会是一个糟糕的编程习惯吗?放一个更好的主意
do_something_4()
do_something_5()
do_something_6()
Run Code Online (Sandbox Code Playgroud)
在每个if/elifs?
我将在这里使用Perl作为比较:
$foo = 5;
print $foo;
Run Code Online (Sandbox Code Playgroud)
将变量设置$foo为5,然后打印变量的内容($foo始终作为访问的通知$foo).
在Tcl中:
set foo 5
puts $foo
Run Code Online (Sandbox Code Playgroud)
与Perl对应物做同样的事情.
为什么Tcl不用"$"设置变量,但需要"$"来访问变量?为什么程序也是如此(例如proc bar {spam eggs} {...})?对我来说,Tcl代码看起来像这样(在伪代码中):
"foo" = 5 # Setting a string?
puts $foo # "$foo" is not defined.
Run Code Online (Sandbox Code Playgroud)
(我的评论只反映了似乎正在发生的事情,而不是正在发生的事情).
我要补充的另一点是清晰度:
set foo foo
Run Code Online (Sandbox Code Playgroud)
是的,我可以随时做set foo "foo",但不是set $foo foo更一致吗?
据我所知,"foo"可以是变量或字符串,具体取决于具体情况,如我上一个例子(set foo foo= set var string)所示,但我没有得到这种语法(也许是因为我习惯了Python .. .)
有没有办法制作一个可以播放任何视频文件的管道(也包含音频)?我试过链接元素,如:
filesrc -> decodebin
Run Code Online (Sandbox Code Playgroud)
随着
queue -> audioconvert -> autoaudiosink
Run Code Online (Sandbox Code Playgroud)
和
queue -> autovideoconvert -> autovideosink
Run Code Online (Sandbox Code Playgroud)
这会导致两个问题:
queue不能链接到autovideoconvert."pad-added"事件实现pad ,特别是当管道同时支持音频和视频时.我想知道如何在不需要的情况下做到这一点gst.parse_launch.另外,我希望pieline能够使用我抛出的任何格式(如playbin),但不能使用playbin,因为我需要链接其他元素(level和volume).
或者,有没有办法将元素(如level)连接到播放箱?
也许我跳进了最后,但我会试一试.
以下是Tkinter的一些有用功能:
Tkinter Canvas小部件是面向对象的绘图画布.绘图的元素本质上是小部件,因为它们可以被移动,修改和绑定到事件.
Tkinter使用绑定来触发回调.该事件以字符串形式传递.可以使用自定义事件轻松创建event_generate.
Tkinter有这种after方法,它等待指定的时间而不冻结GUI.
Tkinter有预定义的字体,如TkDefaultFont颜色和颜色systemButtonFace,这取决于系统.
我的问题是:
这些功能的pyQt等价物(特别是粗体)是什么?
如何将窗口小部件的元素(例如,仅限检查按钮的标签)"绑定"到事件?
更具体地说,我希望能够支持lambda: <some_or_other_setter>,但我希望保持代码清晰,简洁.我必须验证值,所以我需要一个某种类型的setter.我需要使用lambda,因为我需要将回调传递给Tkinter事件.我还需要能够修改绑定之外的属性值.
在我的以下示例中,假设spam_button已全局声明了一个名为的按钮窗口小部件.还假设该类Eggs将具有至少10个属性,而不是以相同的方式访问所有属性(我喜欢一致性).
我能做到的第一种可能的方法是使用getter和setter:
class Eggs(object):
def __init__(self):
self._spam = ''
self.set_spam('Monster')
print self.get_spam()
spam_button.bind('<Enter>', lambda: self.set_spam('Ouch'))
def set_spam(self, spam):
if len(spam) <= 32:
self._spam = spam
def get_spam(self):
return self._spam
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用此方法并具有许多属性,则代码可能会花费太长时间来管理.
我可以做的第二种方法是使用属性,并在回调中使用setter:
class Eggs(object):
def __init__(self):
self._spam = ''
self.spam = 'Monster'
print self.spam
spam_button.bind('<Enter>', lambda: self.set_spam('Ouch'))
def set_spam(self, spam):
if len(spam) <= 32:
self._spam = spam
def get_spam(self):
return self._spam
spam = property(get_spam, set_spam)
Run Code Online (Sandbox Code Playgroud)
直接访问属性时,这种方式比第一种方式简单,但在使用lambda时则不一致.
第三种方法是使用get和set方法创建一个额外的类:
class Spam(object):
def __init__(self):
self._value = …Run Code Online (Sandbox Code Playgroud) 有没有一种方法来获取和更改QTreeView中的活动行(不是QTreeWidget)?主动是指焦点突出显示的行,而不是选定的行。在paint事件中,我可以使用它QStyle.State_HasFocus来获得活动行,但这似乎在其他地方不起作用。
如果我有一个像这样的管道:
gst-launch filesrc location="/home/dk/Music/Vangelis - Alpha.mp3" !
decodebin2 name=dec !
queue ! ffmpegcolorspace ! autovideosink dec. !
queue ! audioconvert ! audioresample ! autoaudiosink
Run Code Online (Sandbox Code Playgroud)
当输入源中没有视频时如何让它播放,但如果有视频则播放视频?我得到的只是:
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Run Code Online (Sandbox Code Playgroud) 函数注释似乎重复已在Python中找到的行为.不仅如此,它们所采用的含义并未以任何方式强制执行,因此它们可用于PEP 3107中记录的以下任何内容:
- 提供打字信息
- 类型检查
- 让IDE显示函数期望和返回的类型
- 函数重载/泛型函数
- 外语桥梁
- 适应
- 谓词逻辑功能
- 数据库查询映射
- RPC参数编组
- 其他信息
- 参数和返回值的文档
甚至是完全不同的东西.
在某种程度上,函数注释让我想起Python的幽默集合中的旧笑话:
事实上,Python已经支持块分隔符:
Run Code Online (Sandbox Code Playgroud)> > if foo: #{ > foo1(); > foo2(); > foo3(); > #}
在那里面
def foo(a: 'x', b: 5 + 6, c: list) -> max(2, 9):
...
Run Code Online (Sandbox Code Playgroud)
没有比以下更有帮助了:
# a: 'x', b: 5 + 6, c: list
def foo(a, b, c): #-> max(2, 9):
...
Run Code Online (Sandbox Code Playgroud)
有人可能认为函数注释是必要的,因为与注释不同,它们可以在代码中访问,例如:
>>> def spam(a: 'eggs') -> 'ni!':
... pass
...
>>> spam.__annotations__
{'a': 'eggs', 'return': …Run Code Online (Sandbox Code Playgroud) 我应该如何使用窗口管理器移动使用PySide创建的窗口?
我看到kdeui有一个NETRootInfo带有moveResizeRequest方法的类,它正是我想要的.下列:
from PySide.QtCore import Qt
from PyKDE4 import kdeui
from PySide.QtGui import QX11Info
import sys
from ctypes import CDLL
Xlib = CDLL('libX11.so.6')
def move_window(window, event):
if event.buttons() & Qt.LeftButton:
pos = event.buttonDownScreenPos(Qt.LeftButton)
Xlib.XUngrabPointer(QX11Info.display(), QX11Info.appTime())
rootinfo = kdeui.NETRootInfo(QX11Info.display(), kdeui.NET.WMMoveResize)
rootinfo.moveResizeRequest(window.winId(), pos.x(), pos.y(), kdeui.NET.Move)
Run Code Online (Sandbox Code Playgroud)
给我:
TypeError: NETRootInfo(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'int'
overload 2: argument 1 has unexpected type 'int'
overload 3: argument 1 has unexpected type …Run Code Online (Sandbox Code Playgroud) 以下哪个类将演示设置实例属性的最佳方法?它们应该根据情况互换使用吗?
class Eggs(object):
def __init__(self):
self.load_spam()
def load_spam(self):
# Lots of code here
self.spam = 5
Run Code Online (Sandbox Code Playgroud)
要么
class Eggs(object):
def __init__(self):
self.spam = self.load_spam()
def load_spam(self):
# Lots of code here
return 5
Run Code Online (Sandbox Code Playgroud) 我试过调用self.setStyleSheet("background: transparent; border: transparent;")QGraphicsView,但它仍然在顶部边缘留下1像素边框.我也曾尝试更换border: transparent;用border-style: none;,但它也不能工作.
以下是问题的屏幕截图:

什么命令将完全删除QGraphicsView的边框?