Fonts Menu PyQt5 Text Editor - python

The Problem:
I am trying to find a way of adding a font style to text that as written by the user of my PyQt5 text editor program. I don't want to code each font into some kind of menu manually and I am wondering if there is a built-in way for the user to select their font style for their text like this (Notepad font picker):
My code currently looks like this:
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Text Editor'
self.left = 10
self.top = 10
self.width = 1080
self.height = 920
self.widget = QWidget(self)
self.lbl = QLabel(self)
self.text = QTextEdit(self.widget)
self.widget.setLayout(QVBoxLayout())
self.widget.layout().addWidget(self.text)
self.setCentralWidget(self.widget)
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
toolBar = self.menuBar()
fileMenu = toolBar.addMenu('File')
editMenu = toolBar.addMenu('Edit')
toolsMenu = toolBar.addMenu('Tools')
helpMenu = toolBar.addMenu('Help')
fontButton = QAction('Configure Editor', self)
fontButton.setShortcut('Ctrl+E')
fontButton.triggered.connect(lambda: self.font_set)
toolsMenu.addAction(fontButton)
self.show()
def font_set(self):
print("Display Fonts")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

Qt has a widget called QFontDialog, and that is perfect for this case, in the following part I show an example of its use:
def font_set(self):
font, ok = QFontDialog.getFont(self.text.font(), self)
if ok:
#QApplication.setFont(font)
self.text.setFont(font)
print("Display Fonts", font)
Note: You must change the following statement:
fontButton.triggered.connect(lambda: self.font_set)
to:
fontButton.triggered.connect(self.font_set)
Screenshot:

Related

Can I use a variable from a function that's inside of another class?

What I'm trying to do is create 1 window with 8 check boxes. The user will click the ones that are relevant and press "Calculate Job". That button should then open a new window that contains buttons/sections/whatever for only the options that were selected in window 1. Sounds ok... I have window 1 set up in class First. When I click the button it shows up the new window, great! But now I need to find a way to say "If any of these sections are selected then do something in window 2".
code:
from PyQt5 import QtWidgets
import sys
import qdarkstyle
class First(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.title = 'Job Price Calculator'
self.left = 100
self.top = 100
self.width = 320
self.height = 350
self.initUI()
self.dialog = Second()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.data_processing = QtWidgets.QCheckBox('Data Processing', self)
self.data_processing.move(50, 50)
self.digital_print = QtWidgets.QCheckBox('Digital Print', self)
self.digital_print.move(50, 100)
self.calculate = QtWidgets.QPushButton('Calculate Job', self)
self.calculate.move(100, 250)
self.calculate.clicked.connect(self.on_button_click)
def on_button_click(self):
self.dialog.show()
class Second(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.title = 'Job Specification'
self.left = 500
self.top = 100
self.width = 1080
self.height = 920
self.initUI_specification()
def initUI_specification(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
if self.data_processing.isChecked():
self.data_processing_info()
if self.digital_print.isChecked():
self.digital_print_info()
def data_processing_info(self):
self.temp_button = QtWidgets.QPushButton('Temp Button', self)
self.temp_button.move(100, 250)
def digital_print_info(self):
self.temp_button2 = QtWidgets.QPushButton('Temp Button 2', self)
self.temp_button2.move(100, 450)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
So what I want to do in class 'Second' is use the checkbox variable of self.data_processing to check if it has been enabled, if it has then I want to pull up a button in the Second gui window.
Is what I'm trying to do possible? Should I be thinking of a different way to do this? I'd like someones opinion on this if possible, and a little bit of guidance. I'd really appreciate the help, I've spent my whole weekend messing around with this and I'm just getting nowhere.
You need to call your second class inside first class when your checkbox is clicked
from PyQt5 import QtWidgets
import sys
import qdarkstyle
class First(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.title = 'Job Price Calculator'
self.left = 100
self.top = 100
self.width = 320
self.height = 350
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.data_processing = QtWidgets.QCheckBox('Data Processing', self)
self.data_processing.move(50, 50)
self.data_processing.stateChanged.connect(self.checkbox_clicked)
self.digital_print = QtWidgets.QCheckBox('Digital Print', self)
self.digital_print.move(50, 100)
self.calculate = QtWidgets.QPushButton('Calculate Job', self)
self.calculate.move(100, 250)
self.calculate.clicked.connect(self.on_button_click)
self.secondClass = Second()
def on_button_click(self):
self.dialog.show()
def checkbox_clicked(self):
if self.data_processing.isChecked():
self.secondClass.data_processing_info()
self.secondClass.show()
else:
self.secondClass.hide() #hide or close - as your requirement
class Second(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.title = 'Job Specification'
self.left = 500
self.top = 100
self.width = 1080
self.height = 920
self.initUI_specification()
def initUI_specification(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
def data_processing_info(self):
self.temp_button = QtWidgets.QPushButton('Temp Button', self)
self.temp_button.move(100, 250)
def digital_print_info(self):
self.temp_button2 = QtWidgets.QPushButton('Temp Button 2', self)
self.temp_button2.move(100, 450)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())

How can I maintain the ability to type after using QLineEdit.selectAll?

I want users of my app to be able to press the button, and then keep typing. With QLineEdit.selectAll(), I am able to select the text entered after Run is pressed, but typing won't do anything. See:
The text is selected due to QLineEdit.selectAll(), but typing won't do anything.
Here's what I have so far:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.title = 'window title'
self.left = 10
self.top = 10
self.width = 400
self.height = 75
self.initUI()
self.layout = QVBoxLayout()
self.line = QLineEdit()
label = QLabel('Enter a WORD:')
run_button = QPushButton('Run')
self.layout.addWidget(label)
self.layout.addWidget(self.line)
self.layout.addWidget(run_button)
widget = QWidget()
widget.setLayout(self.layout)
run_button.clicked.connect(self.on_click)
self.setCentralWidget(widget)
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
def on_click(self):
response = QLabel(self.line.text())
self.layout.addWidget(response)
self.line.selectAll()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
selectAll() is tricky. It looks like the widget has the focus, but it doesn't. Use setFocus(). Order doesn't matter in this case
self.line.setFocus()
self.line.selectAll()
or
self.line.selectAll()
self.line.setFocus()
Further, in this UI, you can hook up the returnPressed signal of self.line to on_click so that when the user presses enter/return in self.line, the on_click method runs.
self.line.returnPressed.connect(self.on_click)
Putting it all together:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.left = 10
self.top = 10
self.width = 400
self.height = 75
self.initUI()
self.layout = QVBoxLayout()
self.line = QLineEdit()
label = QLabel('Enter a WORD:')
run_button = QPushButton('Run')
reset_button = QPushButton('Reset History')
self.label = QLabel()
self.layout.addWidget(label)
self.layout.addWidget(self.line)
self.layout.addWidget(run_button)
self.layout.addWidget(reset_button)
self.layout.addWidget(self.label)
widget = QWidget()
widget.setLayout(self.layout)
run_button.clicked.connect(self.on_click)
self.line.returnPressed.connect(self.on_click)
reset_button.clicked.connect(self.reset_click)
self.setCentralWidget(widget)
def initUI(self):
self.setGeometry(self.left, self.top, self.width, self.height)
def on_click(self):
self.label.setText(self.label.text() + '\n' + self.line.text())
self.line.setFocus()
self.line.selectAll()
def reset_click(self):
self.label.setText('')
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
By using this:
self.line.selectAll()
self.line.grabKeyboard()
It will select the text entered at every push of the button, and you will be able to continue typing without clicking in the textbox.

How to print text beside Editor using PyQt5

I just started to learn how to build a GUI using PyQt5.
I trace some example on the internet and trying to create a GUI for practice.
But I have a problem when I tried to show a text next to the Editor.
I follow the way that I found on the internet but it just not working.
can anyone tell me how to fix it?
I comment the part that I am trying to show the text in my code
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import xml.etree.cElementTree as ET
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt5 textbox - pythonspot.com'
self.left = 10
self.top = 10
self.width = 400
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create textbox
self.textbox = QLineEdit(self)
#self.textbox.setAlignment(Qt.AlignRight)
self.textbox.move(80, 20)
self.textbox.resize(200,40)
self.textbox2 = QLineEdit(self)
self.textbox2.move(80, 80)
self.textbox2.resize(200,40)
#####################################
# the part i am trying to show text #
#####################################
txt1 = QLabel("case indes",self)
txt1.setAlignment(Qt.AlignCenter)
mytext = QFormLayout()
mytext.addRow(txt1,self.textbox) # not showing in Aligned position
mytext.addRow("Case type",tbox2) # not working
# Create a button in the window
self.button = QPushButton('Show text', self)
self.button.move(20,150)
# connect button to function on_click
self.button.clicked.connect(self.on_click)
self.center()
self.show()
#pyqtSlot()
def on_click(self):
textboxValue = self.textbox.text()
textboxValue2 = self.textbox2.text()
QMessageBox.question(self, 'Message - pythonspot.com', "You typed: " + textboxValue + " , second msg is: " + textboxValue2, QMessageBox.Ok, QMessageBox.Ok)
print(textboxValue)
self.textbox.setText("")
self.textbox2.setText("")
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
The layouts are used to manage the position and size of the widgets so you should not use move or resize, you have never established which widget the layout belongs to and finally QMainWindow is a special widget in which you must establish a centralwidget. In the next section there is the solution:
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
central_widget = QWidget()
self.setCentralWidget(central_widget)
# Create textbox
self.textbox = QLineEdit()
self.textbox2 = QLineEdit()
txt1 = QLabel("case indes",self)
txt1.setAlignment(Qt.AlignCenter)
mytext = QFormLayout(central_widget)
mytext.addRow(txt1, self.textbox) # not showing in Aligned position
mytext.addRow("Case type", self.textbox2) # not working
# Create a button in the window
self.button = QPushButton('Show text')
mytext.addRow(self.button)
# connect button to function on_click
self.button.clicked.connect(self.on_click)
self.center()
self.show()

Control window and Display window

I need two PyQt windows to run in the same time on a multiple display machine: one screen for display and the other for input.
In the following example i'm trying to have a simple window with a button and another window with a label. When i push the button, the other windows label text should change:
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QInputDialog,QDesktopWidget,QVBoxLayout,QPushButton,QMainWindow,QAction,QFileDialog,QGroupBox,QGridLayout,QLabel
class Control_Pannel(QWidget):
def __init__(self):
super().__init__()
self.title = 'Main Control Pannel'
self.left = 10
self.top = 10
self.width = 640
self.height = 400
self.text = "Display text"
self.InitializeUI()
def InitializeUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.inputBox = QGroupBox("Display")
layout = QGridLayout()
layout.setColumnStretch(1, 4)
layout.setColumnStretch(2, 4)
button = QPushButton('Push here to change text')
layout.addWidget(button,0,0)
button.clicked.connect(self.on_click)
self.inputBox.setLayout(layout)
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.inputBox)
self.setLayout(windowLayout)
self.show()
app = QApplication(sys.argv)
ex=InputWindows()
sys.exit(app.exec_())
#pyqtSlot()
def on_click(self):
InputWindows.text = "Modified text after button push"
print(InputWindows.text)
InputWindows.update()
##???? here i don't know how to make the changes be reflected in the other window, even if when i print the .text attribute it seems to have changed.
class InputWindows(QDialog):
def __init__(self):
super().__init__()
self.title = 'Display Pannel'
self.left = 5
self.top = 5
self.width = 300
self.height = 300
self.text = "Original Text"
self.InitializeUI()
def InitializeUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.createDisplayGridLayout()
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)
self.show()
def createDisplayGridLayout(self):
self.horizontalGroupBox = QGroupBox("Grid")
layout = QGridLayout()
layout.setColumnStretch(1, 4)
layout.setColumnStretch(2, 4)
self.label = QLabel(self.text)
layout.addWidget(self.label,0,0)
if __name__=='__main__':
app=QApplication(sys.argv)
ex=Control_Pannel()
sys.exit(app.exec_())
With InputWindows.update() i'm getting that first argument of unbound method must have a type QWidget and if i try InputWindows.label.update() i'm getting type object inputwindows has no attribute label
InputWindows is a class, it is an abstraction so you generally have to create an object, I ask you: if you have n Windows InputWindows, to which window would you update the text ?, we would not know, so by simple logic we see that it is wrong to do it. On the other hand, consider each class as a black box where it is stimulated by inputs, and outputs are obtained, in your case Control_Pannel must have an output: the new text, but that output is asynchronous since it changes and must be used when changing , and that in Qt is a signal, so we must create it. On the other hand, InputWindows must receive the information to create a slot that updates the text in the QLabel as shown below:
import sys
from PyQt5 import QtCore, QtWidgets
class Control_Pannel(QtWidgets.QWidget):
sendSignal = QtCore.pyqtSignal(str)
def __init__(self):
super().__init__()
self.title = 'Main Control Pannel'
self.left = 10
self.top = 10
self.width = 640
self.height = 400
self.text = "Display text"
self.InitializeUI()
def InitializeUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.inputBox = QtWidgets.QGroupBox("Display")
layout = QtWidgets.QGridLayout()
layout.setColumnStretch(1, 4)
layout.setColumnStretch(2, 4)
button = QtWidgets.QPushButton('Push here to change text')
layout.addWidget(button,0,0)
button.clicked.connect(self.on_click)
self.inputBox.setLayout(layout)
windowLayout = QtWidgets.QVBoxLayout(self)
windowLayout.addWidget(self.inputBox)
#QtCore.pyqtSlot()
def on_click(self):
text = "Modified text after button push"
self.sendSignal.emit(text)
class InputWindows(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.title = 'Display Pannel'
self.left = 5
self.top = 5
self.width = 300
self.height = 300
self.InitializeUI()
self.setText("Original Text")
#QtCore.pyqtSlot(str)
def setText(self, text):
self.label.setText(text)
def InitializeUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.createDisplayGridLayout()
windowLayout = QtWidgets.QVBoxLayout(self)
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)
def createDisplayGridLayout(self):
self.horizontalGroupBox = QtWidgets.QGroupBox("Grid")
layout = QtWidgets.QGridLayout()
layout.setColumnStretch(1, 4)
layout.setColumnStretch(2, 4)
self.label = QtWidgets.QLabel()
layout.addWidget(self.label, 0, 0)
self.horizontalGroupBox.setLayout(layout)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
ex1 = Control_Pannel()
ex1.show()
ex2 = InputWindows()
ex2.show()
ex1.sendSignal.connect(ex2.setText)
sys.exit(app.exec_())

How do I add table to central widget in PyQt5 keeping my menu bar?

I am having trouble creating a PyQt5 GUI where I want a table of information as the central widget and a menu bar (which I will eventually put sorting options into). My code allows me to have either the table OR the menu but not both, and can't set a central widget because the class is not defined. Hopefully it's just something small I'm missing. Would appreciate any help, thanks.
Here is my code:
class Main(QMainWindow):
def __init__(self,parent = None):
super().__init__()
#self.grid_widget = App(grid)
self.initUI()
def initUI(self):
exitAct = QAction(QIcon('exit.png'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
#self.setCentralWidget(self.grid_widget)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())
class App(QWidget):
def __init__(self, parent = None):
super(App, self).__init__(parent)
self.title = 'PyQt5 table'
self.left = 90
self.top = 90
self.width = 800
self.height = 600
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add layout, add table to grid layout
grid = QGridLayout()
grid.addWidget(self.tableWidget)
self.setLayout(grid)
# Show widget
self.show()
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(len(q1.index))
self.tableWidget.setColumnCount(len(q1.columns))
self.tableWidget.setHorizontalHeaderLabels(heads) #set column names to headers in df
for i in range(len(q1.index)):
for j in range(len(q1.columns)):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(q1.iat[i, j])))
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
self.tableWidget.move(0,0)
# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)
#pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Which produces this:
and ignores my whole tablewidget altogether.
I have commented out the 3rd line in the QMainWindow because it produces an error saying that App is not defined.
How do I set the central widget so that it allows the table to appear as well? Thanks.
the application should only have a single main, to have several only the first one will be executed, and therefore everything that you are after will not be defined, and consequently App is not defined as you indicate.
So the solution is to eliminate the intermediate main and copy it to the final main.
class Main(QMainWindow):
def __init__(self,parent = None):
super().__init__()
self.grid_widget = App(self)
self.initUI()
...
class App(QWidget):
...
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())

Categories