Is it possible to auto add dashes to PyQt5 Line Edit while the user enters the data, for example if the user wants to enter 123456789012345, while the user enters it should be entered like 12345-67890-12345. Also if the user enters - at the right position it should be accepted in. This was fairly achievable in tkinter, using re(question here), but I think it might be able to do the same with Qt too.
if __name__ == '__main__':
app = QApplication(sys.argv)
le = QLineEdit()
le.show()
sys.exit(app.exec_())
Ps: I have designed the GUI inside of Qt Designer.
inputMask : QString
This property holds the validation input mask.
More https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
self.lineEdit = QtWidgets.QLineEdit()
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit.editingFinished.connect(self.editingFinished)
self.lineEdit.setInputMask("999-9999999;.") # +++
grid = QtWidgets.QGridLayout(centralWidget)
grid.addWidget(self.lineEdit)
def editingFinished(self):
print(f"{self.lineEdit.text()} -> {self.lineEdit.text().replace('-', '')}")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setFont(QtGui.QFont("Times", 22, QtGui.QFont.Bold))
w = MainWindow()
w.show()
sys.exit(app.exec_())
Related
This question already has an answer here:
connect function in pyqt5 does not work
(1 answer)
Closed 2 years ago.
I have been trying to implement QTabWidget section in my application, and would like to be able to close the tabs if needed.
I read that I need to set the setTabsCloseable flag to true and add a pyqtSignal but I cannot do so because QObject has no connect attribute.
On the contrary most examples I have found online, mention the use of QtCore.QObject.Connect()
here is a minimum reproducible example:
from PyQt5 import QtWidgets, QtCore, QtWidgets
import sys, os
class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super(Dialog_01,self).__init__()
mainWidget=QtWidgets.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtWidgets.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QtWidgets.QTabWidget()
self.tabWidget.setTabsClosable(True)
# QtCore.QObject.connect(self.chatView, QtCore.SIGNAL('tabCloseRequested(int)'), self.closeTab)
mainLayout.addWidget(self.tabWidget)
myBoxLayout = QtWidgets.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtWidgets.QWidget(),'Tab_01')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
You can super the QtabWidget to incorporate custom functionality and also keep things wrapped up nicely.
I have found it quite common to override classes to suit certain programs with PyQt /Pyside and I strongly suggest you get into the habit!
from PyQt5 import QtWidgets, QtCore, QtWidgets
import sys, os
class QCustomTabWidget (QtWidgets.QTabWidget):
def __init__ (self, parent = None):
super(QCustomTabWidget, self).__init__(parent)
self.setTabsClosable(True)
self.tabCloseRequested.connect(self.closeTab) # connect to method to close
for i in range(1, 10): # add tabs here
self.addTab(QtWidgets.QWidget(), 'Tab %d' % i)
def closeTab (self, currentIndex):
currentQWidget = self.widget(currentIndex)
currentQWidget.deleteLater()
self.removeTab(currentIndex)
class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super(Dialog_01,self).__init__()
mainWidget=QtWidgets.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtWidgets.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QCustomTabWidget()
mainLayout.addWidget(self.tabWidget)
myBoxLayout = QtWidgets.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtWidgets.QTextEdit(),'Tab_01') #also add tabs here
self.tabWidget.addTab(QtWidgets.QTextEdit(),'Tab_02')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
Is there a way (or maybe a different widget i can use (i couldn't find any) so that i can make my combobox look something like the combobox in the picture ?
The picture shows what i want put with font. I would like the options to be my list instedt of the font.
I would like to see all the option (if there are to many to be able to scroll) and select one.
Here is my code so far:
from PyQt5 import QtCore, QtWidgets
import sys
from PyQt5 import QtGui, QtWidgets, QtPrintSupport
class Thired(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Thired, self).__init__(parent)
self.bb = QtWidgets.QComboBox(self)
self.bb.setGeometry(QtCore.QRect(212, 50, 400, 25))
self.bb.setEditable(True)
bpas = ['a','b','c']
self.bb.addItem("")
self.bb.setItemText(0, "")
for bpa in bpas:
self.bb.addItem(bpa)
self.bb.move(50, 200)
self.bb.activated[str].connect(self.style_choice)
def font_choice(self):
font, valid = QtWidgets.QFontDialog.getFont()
if valid:
self.styleChoice.setFont(font)
def style_choice(self, text):
self.styleChoice.setText(text)
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create(text))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
gui = Thired()
gui.show()
app.exec_()
Edit:
I would like that when the whindow opens that all my option are always beeing shown. So that i do not have to press the dropdown arror. In different words: If the window is beeing opened there is a list of bpas, i want to be able to select from the list one of the bpa/option and send a singal that i choose this bpa.
To explain myself a little more (this is not been shown anywhere in the code): bpas = ['a','b','c'] are projects, and I want the user to seleced one of them and after selecting it the programm will load its conneced database. With dropdown it works, but i dont like the way dorpdown looks like with a lot of options :)
You can use a QListWidget with a QLineEdit:
from PyQt5 import QtCore, QtWidgets
import string
class Thired(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Thired, self).__init__(parent)
self.line_edit = QtWidgets.QLineEdit()
self.list_widget = QtWidgets.QListWidget()
options = list(string.ascii_letters)
self.list_widget.addItems(options)
self.list_widget.itemClicked.connect(self.on_itemClicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.line_edit)
lay.addWidget(self.list_widget)
self.resize(640, 480)
#QtCore.pyqtSlot(QtWidgets.QListWidgetItem)
def on_itemClicked(self, item):
self.line_edit.setText(item.text())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
gui = Thired()
gui.show()
sys.exit(app.exec_())
I'm having a problem with PyQt5 where i have a separate ui file(still a python file not .ui) I'm trying to connect a button which would be located in that file however this doesn't work for me for some reason.
Here's my code.
from PyQt5 import QtCore, QtGui, QtWidgets
from gui import Ui_Form
class Main(QtWidgets.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.show()
self.Ui_Form.exit.clicked.connect(self.handle)
def handle(self):
self.print("hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
and here's some code from my auto generated gui file using pyuic:
self.exit = QtWidgets.QPushButton(Form)
self.exit.setGeometry(QtCore.QRect(375, 270, 115, 27))
self.exit.setObjectName("exit")
this same exact procedure has worked for me before in Qt4 so i don't see why it wouldn't work here?
You must use the ui attribute to access the button. You must change:
self.Ui_Form.exit.clicked.connect(self.handle)
to:
self.ui.exit.clicked.connect(self.handle)
Note: Typically when using a Widget template, it names that element as a form and the design class as Ui_Form, so you should use QWidget as a class base.
Complete code:
class Main(QtWidgets.QWidget):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.show()
self.ui.exit.clicked.connect(self.handle)
def handle(self):
self.print("hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
I had a little research regarding this question, but i couldn't find any simple answer or tutorial to do this.
I'm trying to find a widget, that will make transition between pages using QPushButton. I have heard of QStackWidget, But i'm not exactly sure how to use it, I have found many tutorials, but all of them were hard to understand.
So for example, i have a window class:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
How can i add QStackWidget to this class? (Without any layouts)
If i do so, how can i switch to specific page using QPushButton? (With adding multiple widgets in one index)
extra: is it possible to add some kind of effect on transition? like fading, etc.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.stacked_widget = QtGui.QStackedWidget()
self.button = QtGui.QPushButton("Next")
self.button.clicked.connect(self.__next_page)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.stacked_widget)
layout.addWidget(self.button)
widget = QtGui.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.stacked_widget.addWidget(QtGui.QLabel("Page 1"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 2"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 3"))
def __next_page(self):
idx = self.stacked_widget.currentIndex()
if idx < self.stacked_widget.count() - 1:
self.stacked_widget.setCurrentIndex(idx + 1)
else:
self.stacked_widget.setCurrentIndex(0)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Hi I have designed a basic GUI in QT and created a .py file from it.
When the window starts up I want to add another menu item. I have tried a few pieces of code I found on google but nothing seems to work. The code will need to go in the method addAdminMenu()
from PyQt4 import QtGui
import sys
from supplypy.core.windows.main_window import Ui_MainWindow
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
sys.exit(self.app.exec_())
def addAdminMenu(self):
pass
#####Add code here to create a Admin menu####
if __name__ == '__main__':
srm = SRM()
It should be as simple as accessing the menuBar() of the QMainWindow and adding an item, for example: (I removed the Ui_MainWindow lines just because I don't know what it's for -- a Windows requirement?)
from PyQt4 import QtGui
import sys
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.menubar = self.MainWindow.menuBar()
self.MainWindow.show()
self.addAdminMenu()
sys.exit(self.app.exec_())
def addAdminMenu(self):
self.menubar.addMenu('&Admin');
if __name__ == '__main__':
srm = SRM()