Not sure why textbox isn't showing - python

I'm making a simple browser with a search box in PyQt5. This is what I've written:
import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow
from PyQt5.QtWebKitWidgets import QWebView , QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtNetwork import *
import sys
from optparse import OptionParser
class App(QMainWindow):
def initiate(self):
super().initiate()
self.InitUI()
def initUI(self):
self.setWindowTitle('Browser')
self.setGeometry(100, 200, 1000, 2000)
self.searchbox = QLineEdit(self)
self.searchbox.move(20, 20)
self.searchbox.resize(1500,40)
self.go = QPushButton('Go', self)
self.go.move(1810, 20)
self.go.connect(self.gourl)
self.show()
def gourl(self):
url = self.searchbox.text()
class Browser(QWebView):
def __init__(self):
self.view = QWebView.__init__(self)
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
def load(self,url):
self.setUrl(QUrl(url))
App.searchbox.setText(url)
def adjustTitle(self):
self.setWindowTitle(self.title())
app = QApplication(sys.argv)
view = Browser()
box = App()
box.show()
view.show()
view.load("https://duckduckgo.com")
app.exec_()
The browser part loads, but the textbox doesn't show. Python also throws this error:
Traceback (most recent call last):
File "C:\Users\Sid\Desktop\browser.py", line 43, in <module>
view.load("https://duckduckgo.com")
File "C:\Users\Sid\Desktop\browser.py", line 34, in load
App.searchbox.setText(url)
AttributeError: type object 'App' has no attribute 'searchbox'
I don't know why the textbox doesn't show, and I can't understand why the error is being thrown. Can someone please point out the error?
Thanks in advance. :)

What's New in Qt 5.6
https://doc.qt.io/qt-5/whatsnew56.html#removed-functionality
Porting from QtWebKit to QtWebEngine
https://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
import sys
#import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow
#from PyQt5.QtWebKitWidgets import QWebView , QWebPage
#from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebEngine import *
from PyQt5.QtWebEngineWidgets import *
#from PyQt5.QtNetwork import *
#from optparse import OptionParser
class App(QMainWindow):
# def initiate(self):
# super().initiate()
def __init__(self, parent=None):
super().__init__(parent)
self.initUI() # - InitUI -> + initUI
def initUI(self):
self.setWindowTitle('Browser')
self.setGeometry(100, 200, 500, 400)
self.searchbox = QLineEdit("https://stackoverflow.com/questions/57841281/not-sure-why-textbox-isnt-showing", self)
self.searchbox.move(20, 20)
self.searchbox.resize(460,40)
self.go = QPushButton('Go', self)
self.go.move(370, 100)
self.go.clicked.connect(self.gourl) # clicked
self.show()
def gourl(self):
url = self.searchbox.text()
print(f"url = {url}")
self.webview = Browser()
self.webview.load(QUrl(url))
self.webview.show()
class Browser(QWebEngineView): #(QWebView):
windowList = []
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = Browser()
new_window = App()
new_window.setCentralWidget(new_webview)
#new_window.show()
self.windowList.append(new_window)
return new_webview
"""
def __init__(self, parent=None):
super().__init__(parent)
# self.view = QWebView.__init__(self)
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
def load(self,url):
self.setUrl(QUrl(url))
App.searchbox.setText(url)
def adjustTitle(self):
self.setWindowTitle(self.title())
"""
if __name__ == "__main__":
app = QApplication(sys.argv)
# view = Browser()
box = App()
box.show()
# view.show()
# view.load("https://duckduckgo.com")
sys.exit(app.exec_())
Update
without a new window?? if possible
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QAction, QLineEdit,
QMessageBox, QMainWindow, QGridLayout)
from PyQt5.QtWebEngine import *
from PyQt5.QtWebEngineWidgets import *
class App(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.searchbox = QLineEdit("https://stackoverflow.com/questions/57841281/not-sure-why-textbox-isnt-showing", self)
self.go = QPushButton('Go', self)
self.go.clicked.connect(self.gourl)
self.webview = Browser()
self.grid = QGridLayout(centralWidget)
self.grid.addWidget(self.webview, 0, 0, 1, 2)
self.grid.addWidget(self.searchbox, 1, 0)
self.grid.addWidget(self.go, 1, 1)
def gourl(self):
url = self.searchbox.text()
self.webview.load(QUrl(url))
class Browser(QWebEngineView): #(QWebView):
windowList = []
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = Browser()
new_window = App()
new_window.setCentralWidget(new_webview)
#new_window.show()
self.windowList.append(new_window)
return new_webview
if __name__ == "__main__":
app = QApplication(sys.argv)
box = App()
box.setWindowTitle('Browser')
box.resize(600, 500)
box.show()
sys.exit(app.exec_())

Related

Detect Slider move

I want to be able to detect if the slider has moved to a new position and then do some actions in another function called sl. Here is my code:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QScrollBar, QMainWindow, QMenu, QWidget, QGridLayout, \
QMessageBox, QListWidget, QPlainTextEdit, QTableWidget, QTableWidgetItem, QHeaderView
from PyQt5.QtWidgets import QMenuBar, QHBoxLayout, QVBoxLayout, QSlider, QPushButton, QDial, QBoxLayout, QSpacerItem
from PyQt5.QtGui import QFont, QColor, QPixmap, QResizeEvent, QPen
import PyQt5.QtGui as QtGui
import PyQt5.QtCore as QtCore
class ControlWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.layout.setContentsMargins(10, 10, 10, 10)
widget3 = QLabel("Manual")
big_font = QFont('Arial')
big_font.setPointSize(15)
widget3.setFont(big_font)
self.layout.addWidget(widget3, stretch=1)
widget2 = ManualWidget()
self.layout.addWidget(widget2, stretch=4)
self.setLayout(self.layout)
class ManualWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QHBoxLayout()
right_widget = QWidget()
right_layout = QVBoxLayout()
right_top_widget = QWidget()
self.sld1 = self.slider('V1:', 0, 100)
right_top_layout = QVBoxLayout()
right_top_layout.setContentsMargins(0, 0, 0, 0)
right_top_layout.addWidget(self.sld1, stretch=1)
right_top_widget.setLayout(right_top_layout)
right_layout.addWidget(right_top_widget)
right_widget.setLayout(right_layout)
self.layout.addWidget(right_widget, stretch=1)
self.setLayout(self.layout)
self.layout.addWidget(self.name)
self.layout.addWidget(self.label)
self.layout.addWidget(self.slider)
print(self.sl())
def slider(self, name, low, high, step=10):
self.name = QLabel(str(name))
self.label = QLabel(str(low))
self.slider = QSlider(Qt.Horizontal, self)
self.slider.setMinimum(low)
self.slider.setMaximum(high*step)
self.slider.setValue(low)
self.slider.valueChanged.connect(self.change_value)
self.action = False
self.slider.sliderMoved.connect(self.act1)
def change_value(self):
self.set_point = (float(self.slider.value())) / 10
self.label.setText(str(self.set_point))
def act1(self):
self.action = True
return self.action
def sl(self):
if self.action == True:
x = 3
else:
x = 6
return x
class MainWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
big_font = QFont('Arial')
big_font.setPointSize(10)
bottom_widget = ControlWidget()
self.layout.addWidget(bottom_widget, stretch=10)
self.setLayout(self.layout)
class Window(QMainWindow):
def __init__(self, widget):
QMainWindow.__init__(self)
self.setWindowTitle("Flow test rig")
self.menu = self.menuBar()
self.setCentralWidget(widget)
self.status = self.statusBar()
widget.parent = self
app = QApplication(sys.argv)
main_widget = MainWidget()
win = Window(main_widget)
win.show()
sys.exit(app.exec_())
I tried to detect it with the act1 function, however, self.action is always False, or when it becomes True, it does not reset to False after the first move of the slider. I appreciate it if someone would help me.

Copy selected text from PDF in PyQt5 QWebEngineView

I use PyQt5.QtWebEngineWidgets to display a PDF and I'm trying to get the selected text.
Using hasSelection() and selectedText() works on HTML files, but it fails when the web view displays a PDF file. Any idea how to copy the selected text (apart from using CTRL+C)?
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QMenu
from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineView
class Browser(QWebEngineView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_selection(self):
if self.hasSelection():
print(self.selectedText())
else:
print('No text selected')
def contextMenuEvent(self, event):
self.menu = QMenu()
get_selection = self.menu.addAction('Copy')
get_selection.triggered.connect(self.get_selection)
self.menu.popup(event.globalPos())
class MainWindow(QMainWindow):
def __init__(self):
super(QMainWindow, self).__init__()
self.setWindowTitle("PDF Viewer")
self.setGeometry(0, 28, 1000, 750)
self.webView = QWebEngineView()
self.webView = Browser()
self.webView.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
self.webView.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)
self.setCentralWidget(self.webView)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = MainWindow()
win.show()
win.webView.setUrl(QUrl.fromLocalFile("d:/test.pdf"))
sys.exit(app.exec_())

Problem integrating QFontComboBox in Qmenu

I'm trying to integrate QFontComboBox within Qmenu.
I try to make two things happen when selecting a particular font from the menu:
The Qmenu will close.
print the selected font.
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QIcon, QFont, QCursor
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox, QWidgetAction, QMenu, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Pyside2 FontCombo Box")
self.setGeometry(300,200,300,250)
self.setFontBox()
self.setIcon()
self.show()
def setIcon(self):
appIcon = QIcon("icon.png")
self.setWindowIcon(appIcon)
def setFontBox(self):
self.font_button = QPushButton(self)
self.font_button.setFixedWidth(300)
self.font_button.clicked.connect(lambda: self.setFontmenu())
vbox = QVBoxLayout()
vbox.addWidget(self.font_button)
self.setLayout(vbox)
def setFontmenu(self):
font_menu = QMenu()
font_submenu = QFontComboBox()
font_submenu.setCurrentFont(QFont("Arial"))
objectTest = QObject()
widget = QWidgetAction(objectTest)
widget.setDefaultWidget(font_submenu)
font_menu.addAction(widget)
font_menu.exec_(QCursor.pos())
menu = font_menu
menu.addSeparator()
font_submenu.showPopup()
font_submenu.setFocus()
font_submenu.currentFontChanged.connect(self._changed)
def _changed(self):
font = self.currentFont().family()
print(font)
return
myapp = QApplication(sys.argv)
window = Window()
myapp.exec_()
sys.exit()

How to embed a cmd to pyqt5 application

I tried codes below, but it seemed not work as normal. I cannot use embedded cmd whatever I did. It looks like decorations. I mean I just want to use the cmd like the ordinary one. Here, I paste the code, and any advice would be greatly appreciated.
I develop this app on Python 3.7.3 conda environment, Window10.
import sys
import subprocess
import time
import win32gui
from PyQt5.QtCore import QProcess, Qt
from PyQt5.QtGui import QWindow, QIcon, QFont
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMdiArea, QSplitter, QTextBrowser
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from win32com import client
from win32gui import GetWindowText, EnumWindows,SetForegroundWindow
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.p = QProcess()
self.layout = QVBoxLayout()
self.mdi = QMdiArea()
self.mainSplitter = QSplitter(Qt.Vertical)
self.setCentralWidget(self.mainSplitter)
self.mainSplitter.addWidget(QTextBrowser())
self.initUI()
def initUI(self):
self.runExe()
EnumWindows(self.set_cmd_to_foreground, None)
hwnd1 = win32gui.GetForegroundWindow()
#hwnd1 = win32gui.FindWindow(None, "C:\\Windows\\system32\\calc.exe")
print(hwnd1)
window = QWindow.fromWinId(hwnd1)
container_widge = self.createWindowContainer(window, self)
container_widge.setFocusPolicy(Qt.TabFocus)
container_widge.setFocus()
container_widge.setWindowTitle("ain")
container_widge.setFont(QFont("Times New Roman"))
container_widge.setGeometry(500, 500, 450, 400)
#container_widge.setFocusPolicy()
container_widge.activateWindow()
container_widge.acceptDrops()
container_widge.grabMouse()
container_widge.setMouseTracking(True)
self.mainSplitter.addWidget(container_widge)
self.showMaximized()
#self.setGeometry(200, 200, 700, 700)
#self.show()
def runExe(self):
shell.run("cmd.exe")
time.sleep(1)
def set_cmd_to_foreground(self, hwnd, extra):
"""sets first command prompt to forgeround"""
if "cmd.exe" in GetWindowText(hwnd):
print(hwnd)
SetForegroundWindow(hwnd)
return
def run_script(self, shell, scripts):
"""runs the py script"""
shell.SendKeys(scripts+"{ENTER}")
if __name__ == '__main__':
shell = client.Dispatch("WScript.Shell")
app = QApplication(sys.argv)
ex = Example()
#ex.run_script(shell, "python -m pip list")
#ex.show()
sys.exit(app.exec_())
you can try this I got it from this blog:
import sys
from PyQt5.QtCore import QProcess, QTextStream
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit,
QLineEdit, QVBoxLayout, QHBoxLayout, QWidget, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.process = QProcess(self)
self.output = QTextEdit(self)
self.input = QLineEdit(self)
self.run_command_button = QPushButton("Run Command", self)
layout = QVBoxLayout()
input_layout = QHBoxLayout()
input_layout.addWidget(self.input)
input_layout.addWidget(self.run_command_button)
layout.addLayout(input_layout)
layout.addWidget(self.output)
central_widget = QWidget(self)
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.process.readyReadStandardOutput.connect(self.read_output)
self.run_command_button.clicked.connect(self.run_command)
self.process.start("cmd.exe")
def read_output(self):
stream = QTextStream(self.process)
self.output.append(stream.readAll())
def run_command(self):
command = self.input.text() + "\n"
self.process.write(command.encode())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

Python PYQT Tabs outside app window [why]

Tabs added before app.exec_() is called look and act as any other tabs u met, though if adding another after the app.exec_() call makes the new tab 'detach' from the main app window. Pic below :)
Why? How can I make it move inside the window?
import threading
import time
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTabWidget
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QWidget
class ATry(threading.Thread):
def __init__(self):
super().__init__()
def run(self):
time.sleep(1)
anotherTextEdit = QTextEdit()
anotherLineEdit = QLineEdit()
anotherLayout = QFormLayout()
anotherLayout.addRow(anotherTextEdit)
anotherLayout.addRow(anotherLineEdit)
anotherTab = QWidget()
anotherTab.setLayout(anotherLayout)
md.addTab(anotherTab, "Outside")
app = QApplication(sys.argv)
md = QTabWidget()
aTextEdit = QTextEdit()
aLineEdit = QLineEdit()
layout = QFormLayout()
layout.addRow(aTextEdit)
layout.addRow(aLineEdit)
thisTab = QWidget()
thisTab.setLayout(layout)
md.addTab(thisTab, "Inside")
a = ATry()
a.start()
md.show()
app.exec_()
Screen describing the problem
It works with QTimer or signals:
import sys
import time
from PyQt5.QtCore import QObject
from PyQt5.QtCore import QThread
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTabWidget
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QWidget
class ATry(QThread):
def __init__(self, pointer):
super().__init__()
self.pointer = pointer
def run(self):
time.sleep(2)
self.pointer.emit()
def addTheTab():
anotherTextEdit = QTextEdit()
anotherLineEdit = QLineEdit()
anotherLayout = QFormLayout()
anotherLayout.addRow(anotherLineEdit)
anotherLayout.addRow(anotherTextEdit)
anotherTab = QWidget()
anotherTab.setLayout(anotherLayout)
md.addTab(anotherTab, "Whatever")
class MyQObject(QObject):
trigger = pyqtSignal()
def __init__(self):
super().__init__()
def connect_and_get_trigger(self):
self.trigger.connect(addTheTab)
return self.trigger
def getGFX(self):
app = QApplication(sys.argv)
md = QTabWidget()
md.show()
return app, md
obj = MyQObject()
app, md = obj.getGFX()
addTheTab()
a = ATry(obj.connect_and_get_trigger())
a.start()
# timer = QTimer()
# timer.timeout.connect(proba)
# timer.start(3000)
app.exec_()

Categories

Resources