Consider the following code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class First(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
x = Button(text='somebutton')
x.bind(on_press=lambda*_: print('First press'))
x.bind(on_press=lambda*_: print('Second press'))
self.add_widget(x)
def something(self, *somethingishereignored):
print("I have something")
class FChooser(App):
def build(self):
return First()
if __name__ == '__main__':
FChooser().run()
The behaviour of this code is that, after I press the 'somebutton' button, it prints:
Second press
First press
So, I googled and found that I should use the unbind() function, and I added this:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class First(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
x = Button(text='somebutton')
x.bind(on_press=lambda*_: print('First press'))
x.unbind(on_press=lambda*_: print('First press'))
x.bind(on_press=lambda*_: print('Second press'))
self.add_widget(x)
def something(self, *somethingishereignored):
print("I have something")
class FChooser(App):
def build(self):
return First()
if __name__ == '__main__':
FChooser().run()
but the output doesn't change. It's still the same output. How do I release the bind? This is just a minimal example, and I intend to use this functionality to dynamically bind and unbind a function to a button, to add various functionality to the same button.
The function will not unbind, because you do not refer to the function you bound to. As you use inline lambda, that method reference was not saved, so you can't use it to unbind later.
This will work tho:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class First(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
x = Button(text='somebutton')
x.bind(on_press=self.press1)
x.unbind(on_press=self.press1)
x.bind(on_press=self.press2)
self.add_widget(x)
def press1(self, *args):
print("First press")
def press2(self, *args):
print("Second press")
class FChooser(App):
def build(self):
return First()
if __name__ == '__main__':
FChooser().run()
Or this:
press1 = lambda*_: print('First press')
press2 = lambda*_: print('Second press')
x = Button(text='somebutton')
x.bind(on_press=press1)
x.unbind(on_press=press1)
x.bind(on_press=press2)
Related
AttributeError: type object 'QThread' has no attribute 'create'
Here is my code.
from PyQt5.QtCore import QThread
def fun(num):
print(num)
thread1 = QThread.create(fun)
thread1.start()
But Qt documentation says there is a function called create since Qt 5.10. I am using PyQt5 5.11.3. Someone please help me with this.
You can use worker objects by moving them to the thread using QObject::moveToThread().
from PyQt5.QtCore import QObject, pyqtSignal, QThread, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QPushButton
class Worker(QObject):
valueChanged = pyqtSignal(int) # Value change signal
def run(self):
print('thread id ->', int(QThread.currentThreadId()))
for i in range(1, 101):
print('value =', i)
self.valueChanged.emit(i)
QThread.msleep(100)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.progressBar = QProgressBar(self)
self.progressBar.setRange(0, 100)
layout.addWidget(self.progressBar)
layout.addWidget(QPushButton('Open thread', self, clicked=self.onStart))
# Current thread id
print('main id = ', int(QThread.currentThreadId()))
# Start thread update progress bar value
self._thread = QThread(self)
self._worker = Worker()
self._worker.moveToThread(self._thread) # Move to thread to execute
self._thread.finished.connect(self._worker.deleteLater)
self._worker.valueChanged.connect(self.progressBar.setValue)
def onStart(self):
print('main id -> ', int(QThread.currentThreadId()))
self._thread.start() # Start thread
QTimer.singleShot(1, self._worker.run)
def closeEvent(self, event):
if self._thread.isRunning():
self._thread.quit()
del self._thread
del self._worker
super(Window, self).closeEvent(event)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Window()
w.show()
w.setWindowTitle('Demo moveToThread')
sys.exit(app.exec_())
this is code with player only work fine ; with treeview only also work but together the app crash immediately if i click on radio button to show the treeview.
But if i click open button and the file explorer appear than it work and i can use radiobutton without problem ; i don't know why.
import sys
from PyQt5 import QtGui, QtWidgets, QtCore, uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
from PyQt5.uic import loadUi
class MyApp(QMainWindow):
def __init__(self):
super(MyApp, self).__init__()
loadUi("window.ui", self)
#########################################################################
self.model = QFileSystemModel(self)
self.model.setRootPath(self.model.myComputer())
self.treeview.setModel(self.model)
self.treeview.clicked.connect(self.on_treeView_clicked)
self.folder_open.toggled.connect(self.HideShow) # show/hide treeview
#########################################################################
self.media = QMediaPlayer()
video = QVideoWidget(self.videoShow)
video.setGeometry(QtCore.QRect(0, 0, 439, 282))
self.playButton.clicked.connect(self.playVideo)
self.openbtn.clicked.connect(self.openFile)
self.media.setVideoOutput(video)
#########################################################################
#QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeView_clicked(self, index):
indexItem = self.model.index(index.row(), 0, index.parent())
filePath = self.model.filePath(indexItem)
self.choice.setText(filePath)
def HideShow(self):
if self.folder_open.isChecked() == True:
self.showTree()
if self.folder_open.isChecked() == False:
self.hideTree()
def showTree(self):
self.list.setFixedSize(443, 310)
window.setFixedSize(1200, 320)
def hideTree(self):
self.list.setFixedSize(0, 310)
window.setFixedSize(750, 320)
###########################################################################
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",QDir.homePath())
if fileName != '':
self.media.setMedia(QMediaContent(QUrl.fromLocalFile(fileName)))
def playVideo(self):
fileName = self.choice.text()
self.media.setMedia(QMediaContent(QUrl.fromLocalFile(fileName)))
self.media.play()
#if self.media.state() == QMediaPlayer.PlayingState:
#self.media.pause()
#else:
#self.media.play()
###########################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyApp()
window.setFixedSize(750, 320)
window.show()
sys.exit(app.exec_())
window.ui used : download here
I'm trying to display files and folders like column view in Mac finder.
I was able get the basic structure with the help of ListViews and QFileSystemModel. Then I set the splitter handle as a corner widget for the scroll area. I have two issues here
When I resize the listview, the splitter handle disappears.
Even after setting the splitter handle width to 0, I see spacing between
listviews.
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import os
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class PopulateList(QtGui.QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.setModel()
self.show()
self.ui.splitter.setHandleWidth(0)#not working
self.ui.listView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.ui.listView.setCornerWidget(self.getCornerWidget(self.ui.splitter))
self.ui.closePushButton.clicked.connect(self.close)
self.ui.listView.clicked.connect(self.showSubFiles)
def getCornerWidget(self, splitter):
self.handle=splitter.handle(1)
layout=QtGui.QHBoxLayout(self.handle)
layout.setSpacing(0)
layout.setMargin(0)
for i in range(0,2):
line = QtGui.QFrame(self.handle)
line.setFrameShape(QtGui.QFrame.VLine)
layout.addWidget(line)
return self.handle
def showSubFiles(self, index):
root_path = self.model.fileInfo(index).absoluteFilePath()
self.model1=QtGui.QFileSystemModel()
self.model1.setRootPath(root_path)
self.ui.listView_1.setModel(self.model1)
self.ui.listView_1.setRootIndex(self.model1.index(root_path))
def setModel(self):
root_path=os.path.expanduser("~")
self.model=QtGui.QFileSystemModel()
self.model.setRootPath(root_path)
self.model.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs)
self.ui.listView.setModel(self.model)
self.ui.listView.setRootIndex(self.model.index(root_path))
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(602, 365)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.splitter = QtGui.QSplitter(Form)
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName(_fromUtf8("splitter"))
self.listView = QtGui.QListView(self.splitter)
self.listView.setObjectName(_fromUtf8("listView"))
self.listView_1 = QtGui.QListView(self.splitter)
self.listView_1.setObjectName(_fromUtf8("listView_1"))
self.verticalLayout.addWidget(self.splitter)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.closePushButton = QtGui.QPushButton(Form)
self.closePushButton.setObjectName(_fromUtf8("closePushButton"))
self.horizontalLayout.addWidget(self.closePushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.verticalLayout.setStretch(0, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.closePushButton.setText(_translate("Form", "Close", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
listView=PopulateList()
sys.exit(app.exec_())
Are you able to use QColumnView? It is exactly this.
http://doc.qt.io/qt-5/qcolumnview.html
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
model = QtWidgets.QFileSystemModel()
view = QtWidgets.QColumnView()
view.setModel(model)
model.setRootPath("/")
view.show()
sys.exit(app.exec())
I have this small QT program:
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import sys
class QtZListView(QtGui.QListView):
def __init__(self, *args, **kwargs):
QtGui.QListView.__init__(self, *args, **kwargs)
self.model = QtGui.QStringListModel(['a','b','c'])
self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.setModel(self.model)
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setDragEnabled(True)
def setStringList(self, *args, **kwargs):
return self.model.setStringList(*args, **kwargs)
class mplsubwindow(QtGui.QMdiSubWindow):
def __init__(self, *args, **kwargs):
QtGui.QMdiSubWindow.__init__(self, *args, **kwargs)
self.setWindowTitle("testing")
self.setAcceptDrops(True)
fig = Figure(figsize=(5, 4), dpi=100,
facecolor = self.palette().color(QtGui.QPalette.Background).name()
)
p = FigureCanvas(fig)
self.axes = fig.add_subplot(111)
self.axes.hold(False)
FigureCanvas.setSizePolicy(
self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding
)
FigureCanvas.updateGeometry(self)
fig.tight_layout()
toolbar = NavigationToolbar(p, self)
self.layout().addWidget(toolbar)
self.layout().addWidget(p)
self.resize(400,400)
self.show()
def dragEnterEvent(self, event):
print('entering')
super(mplsubwindow, self).dragEnterEvent(event)
def dragMoveEvent(self, event):
print('drag moving')
super(mplsubwindow, self).dragMoveEvent(event)
def dropEvent(self, event):
print('dropped')
super(mplsubwindow, self).dropEvent(event)
class ExampleApp(QtGui.QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
mainwid = QtGui.QWidget()
layout = QtGui.QGridLayout()
mainwid.setLayout(layout)
self.mdiarea = QtGui.QMdiArea()
self.setCentralWidget(mainwid)
layout.addWidget(self.mdiarea)
sub = mplsubwindow(self.mdiarea)
fig = Figure()
p = FigureCanvas(fig)
sub.layout().addWidget(p)
sub.show()
layout.addWidget(QtZListView())
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
I want to be able to drag one or more items from the list in the bottom into the matplotlib canvas. For some reason only the enter-event is invoked...the remaining drag/drop events seems to be ignored...and furthermore it seems like the QMdiSubWindow does not accept drops even though i set setAcceptDrops(True).
What am I missing here?
You need to accept the event in the dragEnterEvent method or else move and drop events are ignored.
def dragEnterEvent(self, event):
print('entering')
event.accept()
super(mplsubwindow, self).dragEnterEvent(event)
Note that I really want to emphasise you should be adding the widget via QMdiSubWindow.setWidget(). Any of the other methods (like using QMdiSubWindow.layout() or QMdiSubWindow.setCentralWidget()) are not fully supported by MDI windows and will likely lead to other issues down the road. If you feel that setWidget() is not doing what you want, ask a new question detailing the issue so that it can be resolved while still using setWidget().
If I have an open QWebView, I like its default context menu with "Open in New Window" as an option for links. However, I can't seem to find a way to act when the user requests a link be opened in a new window. Overriding the QWebPage.createWindow method doesn't seem to work, because the method is not invoked when the user chooses to open a link in a new window.
Any recommendations? I'm using PyQt.
Example code:
class LocalWebPage(QWebPage):
def acceptNavigationRequest(self, webFrame, networkRequest, navigationType):
print '*acceptNavigationRequest**',webFrame, networkRequest, navigationType
return QWebPage.acceptNavigationRequest(self, webFrame, networkRequest, navigationType)
def createWindow(self, windowType):
print '--createWindow', windowType
return QWebPage.createWindow(self, windowType)
class Browser(Ui_MainWindow, QMainWindow):
def __init__(self, base, name):
...
self.page = LocalWebPage()
self.webViewMain = QWebView(self.centralwidget)
self.webViewMain.setPage(self.page)
...
I have the debugging prints in there to verify that createWindow is not being called.
You'll need to call the createWindow method of the QWebView yourself, for example by reimplementing the triggerAction of the QWebPage, something like this:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore, QtWebKit
class MyPage(QtWebKit.QWebPage):
def __init__(self, parent=None):
super(MyPage, self).__init__(parent)
def triggerAction(self, action, checked=False):
if action == QtWebKit.QWebPage.OpenLinkInNewWindow:
self.createWindow(QtWebKit.QWebPage.WebBrowserWindow)
return super(MyPage, self).triggerAction(action, checked)
class MyWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.myPage = MyPage(self)
self.setPage(self.myPage)
def createWindow(self, windowType):
if windowType == QtWebKit.QWebPage.WebBrowserWindow:
self.webView = MyWindow()
self.webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
return self.webView
return super(MyWindow, self).createWindow(windowType)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
main.load(QtCore.QUrl("http://www.example.com"))
sys.exit(app.exec_())
The link that was right-clicked can be found by using hitTestContent in the contextMenuEvent method of the QWebView:
def contextMenuEvent(self, event):
pos = event.pos()
element = self.page().mainFrame().hitTestContent(pos)
link_url = str(element.linkUrl().toString())