还在学习pyqt如何工作.我想动态生成customContextMenu并与函数连接.到目前为止,我得到以下,但连接部分不工作?
import sys
from PyQt4 import QtGui, QtCore
class MainForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
# create button
self.button = QtGui.QPushButton("test button", self)
self.button.resize(100, 30)
# set button context menu policy
self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.connect(self.button, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
self.popMenu = QtGui.QMenu(self)
def on_context_menu(self, point):
self.popMenu.clear()
#some test list for test
testItems = ['itemA', 'itemB', 'itemC']
for item in testItems:
action = self.btn_selectPyFilterPopMenu.addAction("Selected %s" % item)
self.connect(action,QtCore.SIGNAL("triggered()"),self,QtCore.SLOT("printItem('%s')" % item))
self.popMenu.exec_(self.button.mapToGlobal(point))
@pyqtSlot(str)
def printItem(self, item):
print item
def main():
app = QtGui.QApplication(sys.argv)
form = …
Run Code Online (Sandbox Code Playgroud) 想要学习使用文件夹导入模块的正确方法
/garden
__init__.py
utilities.py
someTools.py
/plants
__init__.py
carrot.py
corn.py
Run Code Online (Sandbox Code Playgroud)
里面/plants/__init__.py
我有
__all__ = ['carrot', 'corn']
from . import *
Run Code Online (Sandbox Code Playgroud)
内 carrot.py
def info():
print "I'm in carrot.py"
Run Code Online (Sandbox Code Playgroud)
当我做
import garden
garden.carrot.info()
# got correct result
I'm in carrot.py
Run Code Online (Sandbox Code Playgroud)
我的问题是如何纠正命名空间utilities.py
,例如内部carrot.py
和corn.py
.我想用函数utilities.py
carrot.py
我内心的时候
import utilities as util
# try use it and got global name error
util.someFunc()
# NameError: global name 'util' is not defined #
Run Code Online (Sandbox Code Playgroud)
我可以__init__.py
在plants文件夹中配置它以便导入花园内的所有模块吗?像公用事业和一些工具?所以我不必在carrot.py和corn.py中导入实用程序并且能够使用实用程序?
如何删除 的背景颜色QTreeWidgetItem
或将其重置为默认值?
treeWidgetItem.setBackgroundColor(0, QtGui.QColor('green'))
Run Code Online (Sandbox Code Playgroud)