from PySide2.QtWidgets import QApplication, QMainWindow,QDialog,QLineEdit,QPushButton
from PySide2.QtCore import Qt
import sys
from ui618 import Ui_MainWindow
from numpad import Ui_Dialog
class MyWindow:
def __init__(self) -> None:
self.lastSelectedEditLine = None
# main ui
self.MainWindow = QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
# numpad
self.QDialog = QDialog(self.MainWindow)
self.numpad = Ui_Dialog()
self.numpad.setupUi(self.QDialog)
self.QDialog.setWindowFlags(Qt.Popup)
# bind editline with numpad
self.lineEdits = self.MainWindow.findChildren(QLineEdit)
for item in self.lineEdits:
item.focusInEvent = lambda e,item=item:self.test(e,item)
def save_quit(self):
sys.exit()
def bindbtn(self):
self.ui.quit.clicked.connect(self.save_quit)
def numpad_btn(self,a,item):
if item.text() != '←':
self.lastSelectedEditLine.setText(self.lastSelectedEditLine.text()+item.text())
else:
self.lastSelectedEditLine.setText(self.lastSelectedEditLine.text()[:-1])
def bindNumpad(self):
numpad_btns = self.QDialog.findChildren(QPushButton)
for item in numpad_btns:
item.clicked.connect(lambda ignore='ignore',item=item : self.numpad_btn(ignore,item))
def test(self,e,item):
self.lastSelectedEditLine = item
this = item
x = self.MainWindow.x()
y = self.MainWindow.y() + self.ui.selectX.rect().bottom()
while this.parentWidget().objectName() != 'MainWindow':
x += this.x()
y += this.y()
this = this.parentWidget()
self.QDialog.move(x, y)
self.QDialog.show()
item.clearFocus()
def show(self):
self.MainWindow.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
myWindow.bindbtn()
myWindow.bindNumpad()
sys.exit(app.exec_())
The code works as expected, the numpad shows while focusing in a qlineedit. and can write and delete to target input line.
I am new to pyside2. Is there a better way to implement the numpad? I this way, the cursor is disable, I have to edit at the end of each editline.
It take several seconds while starting, Some one know how to improve it?
Related
I am having trouble understanding why a deep copy of a dictionary reverts back to original values when I run the revert() method once, but when I change values again, and run the revert() method again it changes the copied dictionaries values along with the original.
The dictionary/values are being imported from another file. I can only assume it is because the Test is being imported more than once.
Can someone help explain why this is happening and what can I do to avoid it?
Test.py
import ujson,copy
nested1 = {'1':None, '2':"String"}
nested2 = {'1':0.5123, '2':515}
diction = {'1':nested1, '2':nested2}
copyDiction = ujson.loads(ujson.dumps(diction))
copyOtherDiction = copy.deepcopy(diction)
Main.py
import Test
from PyQt5 import QtCore, QtWidgets, QtGui
class Window(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super(Window,self).__init__(parent)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
layout = QtWidgets.QVBoxLayout()
self.lineEdit = QtWidgets.QLineEdit()
self.button = QtWidgets.QPushButton("Change Value")
self.button.clicked.connect(self.changeMethod)
self.revertbutton = QtWidgets.QPushButton("Revert")
self.revertbutton.clicked.connect(self.revert)
layout.addWidget(self.lineEdit)
layout.addWidget(self.button)
layout.addWidget(self.revertbutton)
widget.setLayout(layout)
def changeMethod(self):
text = self.lineEdit.text()
if text.isdigit():
Test.diction['1']['2'] = int(text)
else:
Test.diction['1']['2'] = text
def revert(self):
print(Test.diction)
Test.diction = Test.copyDiction
print(Test.diction)
print(Test.copyDiction)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I am trying to add new lines to an existing and open Plot.
I wrote a watchdog watching a folder with measurement data. Every few secs there will be a new data file. The Application I try to generate should read the file when triggered by the watchdog and add the data to the plot.
Dynamic plots using QTimer and stuff is easy when updating existing data, but I don't get a hook for new lines.
Also, do I have to use multithreading when I want to run a script while having a plot on _exec()?
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pq
import sys
import numpy as np
import time
class Plot2d(object):
def __init__(self):
self.traces = dict()
self.num = 0
pq.setConfigOptions(antialias=True)
self.app = QtGui.QApplication(sys.argv)
self.win = pq.GraphicsWindow(title='examples')
self.win.resize(1000, 600)
self.win.setWindowTitle('Windowtitle')
self.canvas = self.win.addPlot(title='Plot')
def starter(self):
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def trace(self, name, dataset_x, dataset_y):
if name in self.traces:
self.traces[name].setData(dataset_x, dataset_y)
else:
self.traces[name] = self.canvas.plot(pen='y')
def update(self, i):
x_data = np.arange(0, 3.0, 0.01)
y_data = x_data*i
self.trace(str(i), x_data, y_data)
self.trace(str(i), x_data, y_data)
if __name__ == '__main__':
p = Plot2d()
p.update(1)
p.starter()
time.sleep(1)
p.update(2)
This is what I tried. The update function should be called by the watchdog when new data is available in the dir.
import time as time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
if __name__ == "__main__":
patterns = "*"
ignore_patterns = ""
ignore_directories = False
case_sensitive = True
go_recursively = False
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
def on_created(event):
print(f" {event.src_path} has been created!") #this function should call the plot update
my_event_handler.on_created = on_created
path = (r'C:\Users\...') #path from GUI at some point
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
my_observer.join()
The exec_() method is blocking, so starter() will be too, and will be unlocked when the window is closed, which implies that all the code after starter will only be executed after closing the window. On the other hand you should not use time.sleep in the GUI thread as it blocks the execution of the GUI event loop.
Depending on the technology used, ways of updating elements are offered, in the case of Qt the appropriate way is to use signals so that all the logic of the watchdog will be encapsulated in a QObject that will transmit the information to the GUI through a signal, Then the GUI takes that information to plot.
import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pq
import numpy as np
class QObserver(QtCore.QObject):
dataChanged = QtCore.pyqtSignal(object)
def __init__(self, parent=None):
super(QObserver, self).__init__(parent)
patterns = "*"
ignore_patterns = ""
ignore_directories = False
case_sensitive = True
go_recursively = False
path = r"C:\Users\..." # path from GUI at some point
event_handler = PatternMatchingEventHandler(
patterns, ignore_patterns, ignore_directories, case_sensitive
)
event_handler.on_created = self.on_created
self.observer = Observer()
self.observer.schedule(event_handler, path, recursive=go_recursively)
self.observer.start()
def on_created(self, event):
print(
f" {event.src_path} has been created!"
) # this function should call the plot update
self.dataChanged.emit(np.random.randint(1, 5))
class Plot2d(QtCore.QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.traces = dict()
self.num = 0
pq.setConfigOptions(antialias=True)
self.app = QtGui.QApplication(sys.argv)
self.win = pq.GraphicsWindow(title="examples")
self.win.resize(1000, 600)
self.win.setWindowTitle("Windowtitle")
self.canvas = self.win.addPlot(title="Plot")
def starter(self):
if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
QtGui.QApplication.instance().exec_()
def trace(self, name, dataset_x, dataset_y):
if name in self.traces:
self.traces[name].setData(dataset_x, dataset_y)
else:
self.traces[name] = self.canvas.plot(pen="y")
#QtCore.pyqtSlot(object)
def update(self, i):
x_data = np.arange(0, 3.0, 0.01)
y_data = x_data * i
self.trace(str(i), x_data, y_data)
self.trace(str(i), x_data, y_data)
if __name__ == "__main__":
p = Plot2d()
qobserver = QObserver()
qobserver.dataChanged.connect(p.update)
p.starter()
The following is my code. I am trying to create a serial port thread in my pyQt5 GUI. I checked many examples. This is very helpful one Background thread with QThread in PyQt
I think I made a successful connection but I am confused about the signature.
SelectedItem is the selected port. The GUI is still freezing because of while loop.
I need some help. Thanks!
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
# vim:fileencoding=utf-8
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from TTP_Monitor_GUI import Ui_MainWindow
import serial
import serial.tools.list_ports
import binascii
import glob
class SerialPort(QObject):
def __init__(self, parent = None):
super(SerialPort, self).__init__(parent)
# Explicit signal
newParams = pyqtSignal(str)
#pyqtSlot(str)
def ReadSerialPort(self, port):
#initialization and open the port
with serial.Serial(port, 115200, timeout=1) as ser:
print ("Starting up")
while True:
readOut = 0 #chars waiting from laser range finder
#readOut = ser.readline().decode('ascii')
readOut = ser.read(268) # Reads # Bytes
r = binascii.hexlify(readOut).decode('ascii')
print(r)
self.newParams.emit(r)
#ser.flush() #flush the buffer
if ser.isOpen() == False:
print("Serial Port is Close")
class Main(QMainWindow):
# Explicit Signal
#ser = pyqtSignal(str)
def __init__(self, parent=None):
super(QMainWindow, self).__init__(parent)
#QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btnRefresh.clicked.connect(self.Refresh)
self.ui.btnConnect.clicked.connect(self.Connect)
self.ListFunction(self.SerialPorts())
# Implicit Slot
def Connect(self):
Items = self.ui.listMain.selectedItems()
if len(Items) != 1:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setWindowTitle("BE CAREFULL!")
msg.setText("SELECT ONLY 1 ITEM!...")
msg.exec_()
else:
SelectedItem = Items[0].text()
SelectedItem = SelectedItem.split(" ")[0]
if sys.platform.startswith('win') and SelectedItem[:3] != 'COM':
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setWindowTitle("BE CAREFULL!")
msg.setText("THERE IS A MISTAKE!...")
msg.exec_()
return
# Read Selected Serial Port
self.serialThread = QThread()
self.ser = SerialPort()
self.ser.moveToThread(self.serialThread)
self.ser.newParams.connect(self.serialThread.quit)
#self.serialThread.started.connect(self.ser.ReadSerialPort(SelectedItem))
self.serialThread.started.connect(lambda port=SelectedItem: self.ser.ReadSerialPort(port))
self.serialThread.start()
def Refresh(self):
""" Refresh List Widget """
self.ui.listMain.clear()
if self.ui.radioSerialPorts.isChecked() == True:
self.ListFunction(self.SerialPorts())
elif self.ui.radioTCP.isChecked() == True:
pass
def ListFunction(self, items):
""" Lists items into List Widget """
if type(items) == list and type(items[0]) == str:
#qApp.processEvents() # See Changes on GUI
self.ui.listMain.addItems(items)
else:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setWindowTitle("BE CAREFULL!")
msg.setText("ITEMS ARE WRONG!...")
msg.exec_()
def SerialPorts(self):
""" Lists serial port names """
device = []
description = []
result = []
for port in serial.tools.list_ports.comports():
device.append(port.device)
description.append(port.description)
result.append(port.device + ' - ' + port.description)
return result
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
myapp = Main()
myapp.show()
sys.exit(app.exec_())
provide delay function time.sleep(1) at thread function ReadSerialPort(self, port) inside the while loop äfter the function self.newParams.emit(r).
pyqt UI does not gets sufficient time to execute. so provide delay. it will work.
I am new to PyQt, I am trying to implement slider in PyQt4, but i don't know why code code is not generating any output.
what i want is, create 3 slider to change 3 values dynamically.
Here i am resizing font size of text "hue", "sat", "val".
Is there is any good source to learn slider in PyQt?
here is my code
PyQt4Slider.py
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTiltle("HSV")
self.home()
def home(self):
#hue = 100
#sat = 100
#val = 100
layout = QVBoxLayout()
self.l1 = QLabel("hue")
self.l2 = QLabel("sat")
self.l3 = QLabel("val")
self.l1.setAlignment(Qt.AlignCenter)
self.l2.setAlignment(Qt.AlignCenter)
self.l3.setAlignment(Qt.AlignCenter)
layout.addWidget(self.l1)
layout.addWidget(self.l1)
layout.addWidget(self.l1)
self.sl = QSlider(Qt.Horizontal)
self.s2 = QSlider(Qt.Horizontal)
self.s3 = QSlider(Qt.Horizontal)
self.sl.setMinimum(0)
self.sl.setMaximum(179)
self.sl.setValue(20)
self.sl.setTickPosition(QSlider.TicksBelow)
self.sl.setTickInterval(5)
self.s2.setMinimum(0)
self.s2.setMaximum(255)
self.s2.setValue(100)
self.s2.setTickPosition(QSlider.TicksBelow)
self.s2.setTickInterval(5)
self.s3.setMinimum(0)
self.s3.setMaximum(255)
self.s3.setValue(100)
self.s3.setTickPosition(QSlider.TicksBelow)
self.s3.setTickInterval(5)
layout.addWidget(self.s1)
self.sl.valueChanged.connect(self.valuechange)
layout.addWidget(self.s2)
self.s2.valueChanged.connect(self.valuechange)
layout.addWidget(self.s3)
self.s3.valueChanged.connect(self.valuechange)
self.setLayout(layout)
def valuechange(self):
sizel1 = self.sl.value()
self.l1.setFont("Arial",sizel1)
sizel2 = self.sl.value()
self.l2.setFont("Arial", sizel2)
sizel2 = self.sl.value()
self.l2.setFont("Arial", sizel2)
def main():
app = QApplication(sys.argv)
ex = Window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
here is what i am getting after running code
Ran 0 tests in 0.000s
OK
Process finished with exit code 0
Empty test suite.
Most of the problems in your script are caused by typos. Here is a fixed version that should work okay:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("HSV")
self.home()
def home(self):
layout = QVBoxLayout()
self.l1 = QLabel("hue")
self.l2 = QLabel("sat")
self.l3 = QLabel("val")
self.l1.setAlignment(Qt.AlignCenter)
self.l2.setAlignment(Qt.AlignCenter)
self.l3.setAlignment(Qt.AlignCenter)
layout.addWidget(self.l1)
layout.addWidget(self.l2)
layout.addWidget(self.l3)
self.s1 = QSlider(Qt.Horizontal)
self.s2 = QSlider(Qt.Horizontal)
self.s3 = QSlider(Qt.Horizontal)
self.s1.setMinimum(0)
self.s1.setMaximum(179)
self.s1.setValue(20)
self.s1.setTickPosition(QSlider.TicksBelow)
self.s1.setTickInterval(5)
self.s2.setMinimum(0)
self.s2.setMaximum(255)
self.s2.setValue(100)
self.s2.setTickPosition(QSlider.TicksBelow)
self.s2.setTickInterval(5)
self.s3.setMinimum(0)
self.s3.setMaximum(255)
self.s3.setValue(100)
self.s3.setTickPosition(QSlider.TicksBelow)
self.s3.setTickInterval(5)
layout.addWidget(self.s1)
self.s1.valueChanged.connect(self.valuechange)
layout.addWidget(self.s2)
self.s2.valueChanged.connect(self.valuechange)
layout.addWidget(self.s3)
self.s3.valueChanged.connect(self.valuechange)
self.setLayout(layout)
# set the initial fonts
self.valuechange()
def valuechange(self):
sizel1 = self.s1.value()
self.l1.setFont(QFont("Arial", sizel1))
sizel2 = self.s2.value()
self.l2.setFont(QFont("Arial", sizel2))
sizel3 = self.s3.value()
self.l3.setFont(QFont("Arial", sizel3))
def main():
app = QApplication(sys.argv)
ex = Window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I am currently using PyQt4 to develop a video player GUI, and I use QtGui.QSlider to control the progress of video stream. And I am wondering how can I change the slider's value when video is playing?
import sys, os
import xml.etree.ElementTree as ET
from xml.dom import minidom
from PyQt4 import QtCore, QtGui, uic
from PyQt4.phonon import Phonon
class vidplayer(QtGui.QWidget):
def __init__(self,url,xml_url,parent = None):
self.url = url
super(vidplayer,self).__init__()
self.initUI()
self.getElm_from_XML(xml_url);
def printXMLInfo(self):
itemlist = self.doc.getElementsByTagName('object')
for item in itemlist:
print ("frame-span:"+str(item.attributes['framespan'].value)+
" Event tag: "+ str(item.attributes['name'].value));
print(len(itemlist))
def getElm_from_XML(self,xml_url):
self.doc = minidom.parse(xml_url)
self.clipList = self.doc.getElementsByTagName('object');
print("Reading XML done...\n Have read %s elements.\n" %(len(self.clipList)))
def initUI(self):
## create widgets
# phonon video player and media
self.vp = Phonon.VideoPlayer()
media = Phonon.MediaSource(self.url)
# layout components (boxes)
self.vbox_play = QtGui.QVBoxLayout()
self.hbox_ctrl_vid = QtGui.QHBoxLayout()
self.hbox_ctrl_dec = QtGui.QHBoxLayout()
self.hbox = QtGui.QHBoxLayout()
self.vbox_control = QtGui.QVBoxLayout()
# bottons to control
self.btn_go_prev = QtGui.QPushButton("|<")
self.btn_go_next = QtGui.QPushButton(">|")
self.btn_play = QtGui.QPushButton("Play(Pause)")
self.btn_accept = QtGui.QPushButton("Accept")
self.btn_reject = QtGui.QPushButton("Reject")
# slider to interact with videoplayer
self.sld = QtGui.QSlider(QtCore.Qt.Horizontal,self)
#self.sld = Phonon.SeekSlider(self)
## layout components setup
self.vbox_control.addStretch(1)
self.hbox_ctrl_vid.addStretch(1)
self.hbox_ctrl_dec.addStretch(1)
self.vbox_play.addStretch(1)
self.hbox.addStretch(1)
self.vbox_control.setDirection(QtGui.QBoxLayout.BottomToTop)
self.vbox_play.setDirection(QtGui.QBoxLayout.BottomToTop)
## widgets inits
self.vp.load(media)
self.vp.play()
self.sld.setFocusPolicy(QtCore.Qt.NoFocus)
self.sld.setRange(1,1000)
## widgets assignment
self.hbox_ctrl_vid.addWidget(self.btn_go_prev)
self.hbox_ctrl_vid.addWidget(self.btn_play)
self.hbox_ctrl_vid.addWidget(self.btn_go_next)
self.hbox_ctrl_dec.addWidget(self.btn_accept)
self.hbox_ctrl_dec.addWidget(self.btn_reject)
self.vbox_play.addWidget(self.vp)
self.vbox_play.addWidget(self.sld)
self.vbox_control.addLayout(self.hbox_ctrl_dec)
self.vbox_control.addLayout(self.hbox_ctrl_vid)
self.hbox.addLayout(self.vbox_play)
self.hbox.addLayout(self.vbox_control)
## main setup and display
self.setLayout(self.hbox)
self.setGeometry(300,300,600,400)
self.setWindowTitle('CCNY_SRI TRECVid iSED UI')
self.setWindowIcon(QtGui.QIcon('./icon.png'))
self.show()
## connection set up
self.sld.valueChanged[int].connect(self.sld_changeValue)
self.vp.finished.connect(self.onReachingFinish)
self.btn_play.clicked.connect(self.onClicked_play)
self.btn_go_next.clicked.connect(self.onClicked_nextClip)
self.btn_go_prev.clicked.connect(self.onClicked_prevClip)
###################### callable functions ##################
def sld_changeValue(self,value):
totalT = self.vp.totalTime()
print totalT
newT = totalT*value/1000
self.vp.seek(newT)
def onClicked_play(self):
# BUG: sth wrong with boundary
if self.vp.isPaused():
self.vp.play()
print("resume play")
elif self.vp.isPlaying():
self.vp.pause()
print("pause at",self.sld.value())
elif self.sld.value()<1000:
self.vp.play()
def onClicked_nextClip(self):
print("go next")
def onClicked_prevClip(self):
print("go prev")
def onClicked_acc(self):
print("accepted")
def onClicked_rej(self):
print("rejected")
def onReachingFinish(self):
self.vp.pause()
self.vp.stop()
def main():
app = QtGui.QApplication(sys.argv)
window = vidplayer(sys.argv[1],sys.argv[2])
sys.exit(app.exec_())
if __name__ == '__main__':
main()
There is a dedicated class for that in Phonon: Phonon.SeekSlider:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# creating player resource
self.player = Player()
# adding controls
self.sliders = Sliders(self.player)
class Sliders(QtGui.QWidget):
def __init__(self, player):
QtGui.QWidget.__init__(self)
self.seek_slider = Phonon.SeekSlider(player , self)
class Player(Phonon.MediaObject):
def __init__(self):
Phonon.MediaObject.__init__(self)
self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
self.path = Phonon.createPath(self, self.audioOutput)
This slider will directly pilot the play and its position will be updated during media playing. But, as that's a drawback I'm looking for a workaround, there is no valueChanged signal :-/