I used to have an application in PyQt5, where user could move QGroupBox widget, by pressing mouse button and simply move it. In PyQt5 it works perfectly, but I had to refuse PyQt5 for some reasons and use PyQt4 instead of it. But now I can move my QgroupBox widget only after double click. I use the same piece of code, but in PyQt5 it worked after one click and now only after second one (after first click the whole application's window starts to move). What should I add/edit in order to reach the same result like in PyQt5?
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class WidgetPattern(QGroupBox):
def __init__(self, name, parent=None):
super(WidgetPattern, self).__init__(parent=parent)
self.layout = QVBoxLayout(self)
self._offset = 0
self.widgetLength = 280
self.name = QLabel(parent=self)
self.name.setText(name)
self.name.adjustSize()
self.name.move(self.widgetLength / 2 - self.name.width() / 2, 5)
self.close_button = QToolButton(parent=self)
self.close_button.resize(24, 24)
self.close_button.setAutoRaise(True)
self.close_button.move(self.widgetLength - self.close_button.width(), 0)
self.close_button.setIcon(QIcon.fromTheme("application-exit"))
self.setFixedSize(280, 60)
def mousePressEvent(self, event):
self._offset = event.pos()
def mouseMoveEvent(self, event):
self.move(
QGroupBox.mapToParent(
self,
event.pos() -
self._offset))
class ConcertGUI(QWidget):
def __init__(self, parent=None):
super(ConcertGUI, self).__init__(parent)
widget=WidgetPattern("test")
self._main_layout = QVBoxLayout()
self._main_layout.addWidget(widget, 0)
self.setLayout(self._main_layout)
self.resize(1024, 500)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
gui = ConcertGUI()
gui.show()
sys.exit(app.exec_())
Related
In my program i want to use key press event in list view.When i press the enter key then only my selected text will print..Can any one please tell me how to use key press event for list view.I tried with installEventFilter but this method showing non type error.
Given below is my code:
import sys
from PyQt4 import QtCore,QtGui
class List_View(QtGui.QMainWindow):
def __init__(self, parent=None):
super(List_View, self).__init__(parent)
self.listview = QtGui.QListView()
model = QtGui.QStandardItemModel(self.listview)
for i in ['linux', 'windows', 'mac os']:
model.appendRow(QtGui.QStandardItem(i))
self.listview.setModel(model)
self.listview.entered.connect(self.add_items)
widget = QtGui.QWidget()
self.setCentralWidget(widget)
vlay = QtGui.QVBoxLayout(widget)
vlay.addWidget(self.listview)
self.resize(400, 200)
def add_items(self):
pass #here i need to print particular item in list view
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = List_View()
w.show()
sys.exit(app.exec_())
I think you have not read the docs of the entered signal or you have not understood it, that signal only triggers when the mouse is on the item but for this you must activate the mouse tracking with self.listview.setMouseTracking(True)
A simple solution is to use a QShortcut:
import sys
from PyQt4 import QtCore,QtGui
class List_View(QtGui.QMainWindow):
def __init__(self, parent=None):
super(List_View, self).__init__(parent)
shorcut = QtGui.QShortcut(
QtGui.QKeySequence(QtCore.Qt.Key_Return),
self
)
shorcut.activated.connect(self.on_enter_pressed)
self.listview = QtGui.QListView()
model = QtGui.QStandardItemModel(self.listview)
for i in ['linux', 'windows', 'mac os']:
model.appendRow(QtGui.QStandardItem(i))
self.listview.setModel(model)
widget = QtGui.QWidget()
self.setCentralWidget(widget)
vlay = QtGui.QVBoxLayout(widget)
vlay.addWidget(self.listview)
self.resize(400, 200)
#QtCore.pyqtSlot()
def on_enter_pressed(self):
print("enter press")
ix = self.listview.selectionModel().currentIndex()
print(ix.data())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = List_View()
w.show()
sys.exit(app.exec_())
I'm trying to display the string representation of a list of many float items in a wx.TextCtrl using the SetValue() method.
As soon as the length of the string to be displayed reaches 6151 characters the TextCtrl goes blank and does not display the string. I can still copy portions of the text control as normal and paste them somewhere (such as a text editor) but the characters in the text control itself don't appear on the screen.
Why isn't the text control's value displayed in the text control?
How do I make it display the string if it's longer than 6150 characters?
This happens when setting the text control's value using the SetValue method and when typing in the text control.
Changing the max length for the text control didn't help.
Environment:
Windows 10 (64 bit)
Python 2.7.10
wxPython 3.0
Example code:
import wx
import os
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,-1))
self.control = wx.TextCtrl(self)
self.control.SetMaxLength(10000) #doesn't help
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.control, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show(True)
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
It looks like a bug. According to the this, it should max out at 64K since Windows 98 unless the operating system you have has some kind of odd limit. You can actually increase the number of characters displayed by using one of the wx.TE_RICH style flags.
I was able to replicate your issue on Windows 7 with Python 2.7 and wxPython 3.0.2 using the following code:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
super(MyPanel, self).__init__(parent)
self.text = wx.TextCtrl(self, value="y"*7000)
btn = wx.Button(self, label='Line Length')
btn.Bind(wx.EVT_BUTTON, self.onLength)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(btn, 0, wx.CENTER|wx.ALL, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def onLength(self, event):
""""""
print len(self.text.GetValue())
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(MyFrame, self).__init__(parent=None, title='Test')
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
If I add the wx.TE_RICH flag and call Layout(), I can make it work though:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
super(MyPanel, self).__init__(parent)
self.text = wx.TextCtrl(self, value="y"*7000, style=wx.TE_RICH)
btn = wx.Button(self, label='Line Length')
btn.Bind(wx.EVT_BUTTON, self.onLength)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(btn, 0, wx.CENTER|wx.ALL, 5)
self.SetSizer(sizer)
self.Layout()
#----------------------------------------------------------------------
def onLength(self, event):
""""""
print len(self.text.GetValue())
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(MyFrame, self).__init__(parent=None, title='Test')
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
You should report this bug here: http://trac.wxwidgets.org/
I think my issue is simple, but I have hit a brick wall. I am not a programmer but needed a program to control a laser engraver via Arduino. My Original code was mostly working, but I wanted the ability to select a serial port with a QComboBox so I can make it software available for everyone.
I figured out how to do that with the code below:
import sys
import serial
import time
import serial.tools.list_ports
from PyQt4 import QtGui
from window_test import Ui_MainWindow
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn_laser_poweron.clicked.connect(self.btnFIRE)
self.ui.btn_laser_poweroff.clicked.connect(self.btnOFF)
self.ui.btn_lig_power.clicked.connect(self.btnLIG)
self.ui.btn_cutting_power.clicked.connect(self.btnCUT)
self.ui.btn_power_meter.clicked.connect(self.btnTEST)
self.ui.spinBox.valueChanged.connect(self.PwrLevel)
self.ui.comboBox.activated.connect(self.srlprt)
def srlprt(self):
serial.Serial(str(self.ui.comboBox.currentText()))
def btnFIRE(self):
ser.write("a" + chr(255))
def btnOFF(self):
ser.write("b" + chr(0))
def btnTEST(self):
ser.write("c" + chr(0))
time.sleep(59.5)
ser.write("d" + chr(255))
def btnLIG(self):
ser.write("e" + chr(29))
def btnCUT(self):
ser.write("f" + chr(160))
def PwrLevel(self):
val = self.ui.spinBox.value()
ser.write("g" + chr(val))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
Now my problem is that none of my buttons work because "ser" is not globally defined. I understand that I broke that when I removed "ser = serial.Serial(port=COM3)" when it was above the class definition, but I don't know how to fix it. Any help would be greatly appreciated.
Cheers!
A simple solution would be to just set ser as attribute of your Main instance. Also it couldn't hurt to close the serial connection if it is open before opening a new one, e.g:
import sys
import serial
import time
import serial.tools.list_ports
from PyQt4 import QtGui
from window_test import Ui_MainWindow
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ser = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn_laser_poweron.clicked.connect(self.btnFIRE)
self.ui.btn_laser_poweroff.clicked.connect(self.btnOFF)
self.ui.btn_lig_power.clicked.connect(self.btnLIG)
self.ui.btn_cutting_power.clicked.connect(self.btnCUT)
self.ui.btn_power_meter.clicked.connect(self.btnTEST)
self.ui.spinBox.valueChanged.connect(self.PwrLevel)
self.ui.comboBox.activated.connect(self.srlprt)
def srlprt(self):
if self.ser:
self.ser.close()
self.ser = serial.Serial(str(self.ui.comboBox.currentText()))
def btnFIRE(self):
self.ser.write("a" + chr(255))
def btnOFF(self):
self.ser.write("b" + chr(0))
def btnTEST(self):
self.ser.write("c" + chr(0))
time.sleep(59.5)
self.ser.write("d" + chr(255))
def btnLIG(self):
self.ser.write("e" + chr(29))
def btnCUT(self):
self.ser.write("f" + chr(160))
def PwrLevel(self):
val = self.ui.spinBox.value()
self.ser.write("g" + chr(val))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
How can i make to close the pop-up Dialog and its QMessageBox.information when the ok button is pressed
I got this code from here
i am using it as pop-up dialogbox in my module. The dialog box opens another QMessageBox.information with its standard ok button
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import serial
from serial.serialutil import SerialException
from serialutils import full_port_name, enumerate_serial_ports
class ListPortsDialog(QDialog):
def __init__(self, parent=None):
super(ListPortsDialog, self).__init__(parent)
self.setWindowTitle('List of serial ports')
self.ports_list = QListWidget()
self.tryopen_button = QPushButton('Try to open')
self.connect(self.tryopen_button, SIGNAL('clicked()'),
self.on_tryopen)
layout = QVBoxLayout()
layout.addWidget(self.ports_list)
layout.addWidget(self.tryopen_button)
self.setLayout(layout)
self.fill_ports_list()
def on_tryopen(self):
cur_item = self.ports_list.currentItem()
if cur_item is not None:
fullname = full_port_name(str(cur_item.text()))
try:
ser = serial.Serial(fullname, 38400)
ser.close()
QMessageBox.information(self, 'Success',
'Opened %s successfully' % cur_item.text())
except SerialException, e:
QMessageBox.critical(self, 'Failure',
'Failed to open %s:\n%s' % (
cur_item.text(), e))
def fill_ports_list(self):
for portname in enumerate_serial_ports():
self.ports_list.addItem(portname)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = ListPortsDialog()
form.show()
app.exec_()
i Wanted to close both window 3 and window 2 when the ok button is pressed
Since it is using QDialog after QMessageBox.information(self, 'Success', 'Opened %s successfully' % cur_item.text()) , you can simply say self.accept(). That should close 2 and 3 windows.
I wrote a simple application in Python and PySide. When I run it, SIGNALs are not working.
The application starts without errors.
from PySide.QtCore import *
from PySide.QtGui import *
import sys
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
dial = QDial()
dial.setNotchesVisible(True)
spinbox = QSpinBox()
layout = QHBoxLayout()
layout.addWidget(dial)
layout.addWidget(spinbox)
self.setLayout(layout)
self.connect(dial, SIGNAL("valueChaged(int)"), spinbox.setValue)
self.connect(spinbox, SIGNAL("valueChaged(int)"), dial.setValue)
self.setWindowTitle("Signals and Slots")
# END def __init__
# END class Form
def main():
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
# END def main
if __name__ == '__main__':
main()
# END if
I am using:
Pyside 1.2.2;
Python 2.7.6;
OS Centos; Windows 7
I am running the application with:
Sublime Text 3 and Eclipse Luna;
How can I make SIGNALs working?
Your signal name is incorrect;
Incorrect :
valueChaged (int)
Correct :
valueChanged (int)
Test it, work fine;
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class QFormDialog (QDialog):
def __init__(self, parent = None):
super(QFormDialog, self).__init__(parent)
self.myQial = QDial()
self.myQSpinbox = QSpinBox()
self.myQHBoxLayout = QHBoxLayout()
self.myQial.setNotchesVisible(True)
self.myQHBoxLayout.addWidget(self.myQial)
self.myQHBoxLayout.addWidget(self.myQSpinbox)
self.setLayout(self.myQHBoxLayout)
self.connect(self.myQial, SIGNAL('valueChanged(int)'), self.myQSpinbox.setValue)
self.connect(self.myQSpinbox, SIGNAL('valueChanged(int)'), self.myQial.setValue)
self.setWindowTitle('Signals and Slots')
if __name__ == '__main__':
myQApplication = QApplication(sys.argv)
myQFormDialog = QFormDialog()
myQFormDialog.show()
myQApplication.exec_()
Note : PyQt4 & PySide is same way to implemented.