AttributeError: 'QTextBrowser' object has no attribute 'setFrame' [duplicate] - python

This question already has answers here:
PyQt6: AttributeError Qt [duplicate]
(1 answer)
How to check MouseButtonPress event in PyQt6?
(2 answers)
Qt module alternative for PyQt6
(2 answers)
Closed 18 days ago.
This post was edited and submitted for review 18 days ago.
What is wrong in the code? I am new to QtPy6 and trying to make a vritual book with it.
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextBrowser, QFrame, QLineEdit, QPushButton
class Book(QMainWindow):
def __init__(self):
super().__init__()
self.text_browser = QTextBrowser(self)
self.output_browser = QTextBrowser(self)
self.frame = QFrame(self)
self.frame.setFrameShape(QFrame.Box)
self.frame.setLineWidth(2)
self.line_edit = QLineEdit(self)
self.save_button = QPushButton("Save", self)
self.text_browser.setFrame(self.frame)
self.setCentralWidget(self.text_browser)
with open("book.html") as f:
book_content = f.read()
self.text_browser.setHtml(book_content)
self.line_edit.returnPressed.connect(self.insert_text)
self.save_button.clicked.connect(self.save_file)
def insert_text(self):
text = self.line_edit.text()
self.output_browser.append(text)
def save_file(self):
with open("output.txt", "w") as f:
f.write(self.output_browser.toPlainText())
app = QApplication(sys.argv)
book = Book()
book.show()
sys.exit(app.exec_())
when I run this I get an error showing
AttributeError: 'QTextBrowser' object has no attribute 'setFrame'
what am I doing wrong here? is there any alternative for this issue.

Related

what do we need the Parent parameter in QMainWindow() for? [duplicate]

This question already has answers here:
In PyQT why somes widgets needs the "self" parameter before calling them, while others don't
(3 answers)
What is parent for in Qt?
(2 answers)
Closed 24 days ago.
I'm relatively new to Python and am currently working on the QtWidget. In my online course we created our own class that inherits from QtWidgets.QMainWindow. When creating the constructor, we created a parameter called Parent, what do we need it for? I've also read the official documentation, but I can't make any sense of it.
This is the code:
import sys
from qtpy import QtWidgets
from ui.mainwindow import Ui_MainWindow
app = QtWidgets.QApplication(sys.argv)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent = None):
super().__init__(parent)
self.setWindowTitle("Test")
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.button.clicked.connect(self.on_button_click)
def on_button_click(self):
self.weight = self.ui.weight.value()
self.height = self.ui.height.value()
if self.weight and self.height != 0:
self.rechnung = (self.weight) / (self.height ** 2)
self.ui.result.setText(f"Dein BMI hat einen Wert von {round(self.rechnung, 2)}")
else:
self.ui.result.setText("Deine eingaben dürfen keine 0 enthalten")
window = MainWindow()
window.show()
sys.exit(app.exec_())
we pass this parameter to the QtWidgets.QMainWindow() class, but for what?

buttonClicked() method for button on QMessageBox [duplicate]

This question already has answers here:
How to capture button click from customized QMessageBox?
(3 answers)
Closed 2 years ago.
I want to call "removeDuplicate" method when 'Ok' button on QMessageBox is clicked. But when I click the button the method doesn't execute. What should I do?
Here is my code snippet :
def removeDuplicate(self):
curItem = self.listWidget_2.currentItem()
self.listWidget_2.takeItem(curItem)
def error_popup(self):
msg=QtWidgets.QMessageBox()
msg.setText("You can't select more than one wicket-keeper.")
msg.setWindowTitle(" ")
msg.setIcon(QtWidgets.QMessageBox.Critical)
x = msg.exec_()
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.buttonClicked.connect(self.removeDuplicate)
Try it:
import sys
from PyQt5.Qt import *
from PyQt5 import QtGui, QtCore, QtWidgets
class Window(QWidget):
def __init__(self):
super().__init__()
self.error_popup()
def removeDuplicate(self):
print('def removeDuplicate(self): ...')
# curItem = self.listWidget_2.currentItem()
# self.listWidget_2.takeItem(curItem)
def error_popup(self):
msg = QMessageBox.critical(
self,
'Title',
"You can't select more than one wicket-keeper",
QMessageBox.Yes | QMessageBox.Cancel
)
if msg == QMessageBox.Yes:
# msg.buttonClicked.connect(self.removeDuplicate)
print('Ok')
self.removeDuplicate()
if __name__ == "__main__":
App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec_())

Getting value from QDialog and just before terminate file [duplicate]

This question already has answers here:
Python - Classes and OOP Basics
(6 answers)
What is the difference between objects and classes in Python
(5 answers)
Closed 3 years ago.
I have 2 python files, from one file want to call and run a QDialog with Qlistview. I want to contruct in pythonic way getting value of index just before terminating first python file.
Structure of files:
---Widgetclass.py
---Class_test.py
The code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
self.button = QtWidgets.QPushButton("Okay")
self.listView = QtWidgets.QListView()
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(5)
for text in ("One", "two", "Three", "Four",
"Five etc.."):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
Code_Group = QtWidgets.QGroupBox(self)
Code_Group.setTitle("&Test")
Code_Group.setLayout(lay)
Vlay = QtWidgets.QVBoxLayout(self)
Vlay.addWidget(Code_Group)
Vlay.addWidget(self.button, alignment=QtCore.Qt.AlignCenter)
Vlay.setSizeConstraint(Vlay.SetFixedSize)
self.listView.selectionModel().currentChanged.connect(self.on_row_changed)
self._INDEX = 0
def on_row_changed(self, current, previous):
self._INDEX = current.row()
print('Row %d selected' % current.row())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
The constructor:
from Widgetclass import Widget as ls
from PyQt5 import QtCore, QtGui, QtWidgets
def value():
print('Row index:', ls._INDEX, ':') #<---- Value not callable?
ls.close()
subwindow=ls()
subwindow.setWindowModality(QtCore.Qt.ApplicationModal)
subwindow.button.clicked.connect(value)
subwindow.exec_()
Error getting:
AttributeError: type object 'Widget' has no attribute '_INDEX'
I want to achieve value and close the file. but seems not working?!

Need Switching between 2 Mainwindows in Pyqt5 [duplicate]

This question already has answers here:
How to switch between two PyQt5 MainWindow widgets
(2 answers)
Closed 3 years ago.
I have created 2 separate MainWindow(in 2 different programs). Now I need to switch between these 2 windows. Example: When a button in MainWindow1 is clicked , MainWindow2 should be opened and MainWindow1 should be closed and vice versa. Please help!
Try it:
import sys
from PyQt5 import Qt
class MainWindow1(Qt.QMainWindow):
def __init__(self):
Qt.QMainWindow.__init__(self)
self.mainWindow1()
def mainWindow1(self):
self.setFixedSize(300, 300)
self.setStyleSheet('background-color : rgb(255,255,255);')
self.setWindowTitle('MainWindow1')
self.pushButton = Qt.QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(255,0,0); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.A)
def A(self):
self.cams = MainWindow2()
self.cams.show()
self.close()
class MainWindow2(Qt.QMainWindow):
def __init__(self):
Qt.QMainWindow.__init__(self)
self.setFixedSize(500, 500)
self.setStyleSheet('background-color : rgb(255,0,0);')
self.setWindowTitle('MainWindow2')
self.pushButton = Qt.QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.B)
def B(self):
self.cams = MainWindow1()
self.cams.show()
self.close()
if __name__ == '__main__':
app = Qt.QApplication(sys.argv)
w = MainWindow1()
w.show()
sys.exit(app.exec_())

PyQT4 TextBrowser Set Text Alighment [duplicate]

This question already has answers here:
Aligning text in QTextEdit?
(2 answers)
Closed 4 years ago.
I am doing some random choice picker where there's a place to display info user keyed in. (which I used text browser), How do I set the text to be always align center?
self.textBrowser.setText(newList + "\n\nanalyzing in ...")
QtTest.QTest.qWait(1000)
self.textBrowser.setText(newList + "\n\nanalyzing in ..." + "\n\n 3")
QTextBrowser Class Inherits: QTextEdit
QTextEdit::setAlignment(Qt::Alignment)
Sets the alignment of the current paragraph to a. Valid alignments are Qt::AlignLeft, Qt::AlignRight, Qt::AlignJustify and Qt::AlignCenter (which centers horizontally).
Sorry, I have PyQt5:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Win(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.i = 5
self.textBrowser = QtWidgets.QTextBrowser()
self.textBrowser.setAlignment(QtCore.Qt.AlignCenter) # <-------------
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.textBrowser)
self.setLayout(layout)
timer = QtCore.QTimer(self, interval=1000, timeout=self.print_out)
timer.start()
def print_out(self, string="Hello world!"):
self.textBrowser.append(string + "\n\nanalyzing in ...\n {}".format('----' * self.i))
self.i += 1
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
myWin = Win()
myWin.show()
sys.exit(app.exec_())

Categories

Resources