Answer the question
In order to leave comments, you need to log in
PyQt5 displaying the result of a function in a widget window?
Good afternoon!
Using pyqt5 I am writing a widget. The simplest: two buttons and an information display window (QTextBrowser) (this Browser may not be used correctly).
Button 1 starts the parser.
Button 2 "exit" (everything is OK with this, figured it out)
Everything is OK with the parser execution code. Works. Writes the result to both the database and the file if necessary.
But I just can’t make it so that every time you press button 1, the result of the parsec is displayed not on the command line, but in the window in the widget. I am at war with QTextBrowser, but it turns out nothing.
Tell me where to dig or an example of the button code.
Thank you.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from grab import Grab
from grab import transport
from grab.transport import curl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication, QMainWindow, QTextBrowser)
from PyQt5.QtGui import QTextDocument
def res():
g = Grab()
g.go('http://google.com/')
global a; a = g.doc.select('//p').text()
if (a.startswith("Не Спать")):
return ((a[9:]))
else:
return("Cпать")
pass
class ConfigProgram(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.section1 = Section(self)
self.stack = None
self.build_widget()
def build_widget(self):
self.setWindowTitle('Фия')
# Виджет в котором мы выбираем раздел настроек
self.tree = QTextBrowser()
self.tree.setMaximumWidth(500)
self.tree.setMinimumHeight(100)
a = res()
self.tree.setText(str(a))
#url = QtCore.QUrl('c.txt')
#tree.setSource(url)
self.stack = QtWidgets.QStackedWidget()
self.stack.addWidget(self.section1)
button_panel = self.create_button_panel()
main_layout = QtWidgets.QGridLayout() # Главный компановщик элементов окна
main_layout.addWidget(self.tree, 0, 1)
main_layout.addWidget(self.stack, 1, 1)
main_layout.addWidget(button_panel, 1, 1)
self.setLayout(main_layout)
def create_button_panel(self):
""" Создает виджет, в котором размещается кнопка выхода """
button_panel = QtWidgets.QWidget()
layout = QtWidgets.QHBoxLayout()
layout.addStretch(1)
okButton = QtWidgets.QPushButton('OK')
okButton.clicked.connect(self.buttonClicked)
cancelButton = QtWidgets.QPushButton('Закрыть')
cancelButton.clicked.connect(self.close)
layout.addWidget(okButton)
button_panel.setLayout(layout)
layout.addWidget(cancelButton)
button_panel.setLayout(layout)
return button_panel
def buttonClicked(self):
print((a[9:]))
sender = self.sender()
class Section(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Section, self).__init__(parent)
self.line_edit = []
self.build_widget()
def build_widget(self):
form = QtWidgets.QFormLayout()
self.setLayout(form)
app = QtWidgets.QApplication([])
window = ConfigProgram()
window.show()
app.exec_()
Answer the question
In order to leave comments, you need to log in
1) there is a special button for the code when submitting a question (3 dots and select code)
2) at least write how exactly they tried to set the text in the form. Well, for example like this:
def buttonClicked(self):
print((a[9:]))
window.tree.setText(str(a[9:]))
sender = self.sender()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question