I create 2 windows testWidget and win2. First in testWidget, I click "Input maesure data " in Start menu, then I want to input number in LineEdit of L1(m) in win2, press "Analyze" and shows it in the testWidget. I did it, but it will pop up testWidget again. How to solve it?
Thanks!
import sys
from PyQt4 import QtGui,QtCore
class testWidget(QtGui.QMainWindow):
def __init__(self):
super(testWidget, self).__init__()
self.setGeometry(25,150,1200,700)
self.setWindowTitle('test')
extractAction_1 = QtGui.QAction('&Input maesure data',self)
extractAction_1.triggered.connect( self.newWindow)
self.linetest = QtGui.QLineEdit()
self.linetest.setText("0.0")
layoutV = QtGui.QVBoxLayout()
layoutV.addWidget(self.linetest)
widget = QtGui.QWidget()
widget.setLayout(layoutV)
self.setCentralWidget(widget)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&Start')
fileMenu.addAction(extractAction_1)
def newWindow(self):
self.myOtherWindow = win2()
self.myOtherWindow.show()
def showtex(self,text_LT):
self.linetest.clear()
self.linetest.setText(text_LT)
class win2(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("Set parameters")
self.setGeometry(150,300,300,200)
b2 = QtGui.QPushButton("Analyze")
b2.clicked.connect(self.getre)
layoutV = QtGui.QVBoxLayout()
layoutH = QtGui.QHBoxLayout()
lab1 = QtGui.QLabel("L1(m):")
self.line1 = QtGui.QLineEdit()
self.line1.setText("50")
lab2 = QtGui.QLabel("L2(m):")
self.line2 = QtGui.QLineEdit()
self.line2.setText("0.0")
self.line3test = QtGui.QLineEdit()
layoutH.addWidget(lab1)
layoutH.addWidget(self.line1)
layoutH.addWidget(lab2)
layoutH.addWidget(self.line2)
layoutV.addLayout(layoutH)
layoutV.addWidget(b2)
layoutV.addWidget(self.line3test)
self.widget = QtGui.QWidget()
self.widget.setLayout(layoutH)
self.widget.setLayout(layoutV)
self.setCentralWidget(self.widget)
self.winma=testWidget()
def getre(self):
text_LT = self.line1.text()
self.winma.showtex(text_LT)
self.winma.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
GUI = testWidget()
GUI.show()
app.exec_()
You need to set a global variable for acessing.
GUI = None
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
GUI = testWidget()
GUI.show()
app.exec_()
And, let the subwindow pass value to the GUI.
def getre(self):
text_LT = self.line1.text()
GUI.linetest.setText(text_LT)
Related
i am new in PyQt but i have a problem i can't solve. I am trying to get text from second window and set it to field, so when i close second window i can print it form first main window, but my "AnotherWindow" button won't fire event and i really don't know why? Here is code. Can anyone guide me?
Thanks
class AnotherWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(1200, 600)
self.text = "basetext"
self.layoutf = QFormLayout()
self.buttonf = QPushButton("get text")
self.buttonf.clicked.connect(lambda: self.getText)
self.line = QLineEdit()
self.layoutf.addRow(self.buttonf,self.line)
self.widgetf = QWidget()
self.widgetf.setLayout(self.layoutf)
self.setCentralWidget(self.widgetf)
def getText(self):
print(self.line.text)
self.text = self.line.text
self.close()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.w = None # No external window yet.
self.mainLayout = QGridLayout()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.button1 = QPushButton("Push ")
self.button1.clicked.connect(self.printFromSecondWindow)
self.mainLayout.addWidget(self.button,0,0)
self.mainLayout.addWidget(self.button1, 0, 1)
self.widget = QWidget()
self.widget.setLayout(self.mainLayout)
self.setCentralWidget(self.widget)
def show_new_window(self):
if self.w is None:
self.w = AnotherWindow()
self.w.show()
else:
self.w.close() # Close window.
self.w = None # Discard reference.
def printFromSecondWindow(self):
print(self.w.text)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
The problem is with self.buttonf.clicked.connect(...). This call attaches a function to the "clicked" action on the button. The function is called without parameters and the return is simply discarded. In your case, lambda: self.get_text is a function that does nothing but return the address of the self.get_text method. Since get_text doesn't need any additional parameters, you can bind it directly to this slot.
self.buttonf.clicked.connect(self.get_text)
You also have a bug later on where you need to call the text method. With these two changes, the working program is
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.Qt import *
class AnotherWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(1200, 600)
self.text = "basetext"
self.layoutf = QFormLayout()
self.buttonf = QPushButton("get text")
self.buttonf.clicked.connect(self.getText)
self.line = QLineEdit()
self.layoutf.addRow(self.buttonf,self.line)
self.widgetf = QWidget()
self.widgetf.setLayout(self.layoutf)
self.setCentralWidget(self.widgetf)
def getText(self):
print("the info", self.line.text())
self.text = self.line.text()
self.close()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.w = None # No external window yet.
self.mainLayout = QGridLayout()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.button1 = QPushButton("Push ")
self.button1.clicked.connect(self.printFromSecondWindow)
self.mainLayout.addWidget(self.button,0,0)
self.mainLayout.addWidget(self.button1, 0, 1)
self.widget = QWidget()
self.widget.setLayout(self.mainLayout)
self.setCentralWidget(self.widget)
def show_new_window(self):
if self.w is None:
self.w = AnotherWindow()
self.w.show()
else:
self.w.close() # Close window.
self.w = None # Discard reference.
def printFromSecondWindow(self):
print(self.w.text)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
Instantiate the second window once in the main window constructor. self.w = AnotherWindow (self)
When creating an instance of the second window, self.w = AnotherWindow (self) - self is passed as a parent, so that when the main window is closed, the second window also closes.
To get text from a QLineEdit widget - apply QString text() const, more https://doc.qt.io/qt-5/qlineedit.html#text-prop
You did not show the method printFromSecondWindow in which, as I understand it, you wanted to show what you intended.
Try it:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.Qt import *
class AnotherWindow(QMainWindow):
def __init__(self, parent=None):
super(AnotherWindow, self).__init__(parent)
self.widgetf = QWidget()
self.setCentralWidget(self.widgetf)
# self.resize(1200, 600)
self.text = "basetext"
self.layoutf = QFormLayout(self.widgetf)
self.buttonf = QPushButton("get text")
# self.buttonf.clicked.connect(lambda: self.getText) # ??? lambda
self.buttonf.clicked.connect(self.getText)
self.line = QLineEdit()
self.layoutf.addRow(self.buttonf,self.line)
def getText(self):
print(self.line.text()) # ! .text()
self.text = self.line.text() # ! .text()
self.close()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.widget = QWidget()
self.setCentralWidget(self.widget)
# ? self.w = None # No external window yet.
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.button1 = QPushButton("Push ")
self.button1.clicked.connect(self.printFromSecondWindow)
self.mainLayout = QGridLayout(self.widget)
self.mainLayout.addWidget(self.button, 0, 0)
self.mainLayout.addWidget(self.button1, 0, 1)
self.w = AnotherWindow(self) # +++
def show_new_window(self):
# if self.w is None:
# self.w = AnotherWindow()
self.w.show() # !
# else:
# self.w.close() # Close window.
# self.w = None # Discard reference.
def printFromSecondWindow(self): # +++
print(self.w.text)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I have a scenario where one window is running in python Pyqt5. I want that when certain event happens another window also opens up.
I have written a code which I suppose should run fine but when event occurs to open other GUI, I gets an error.
My code:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
app = QApplication(sys.argv)
win = QWidget()
l1 = QLabel("URL:")
url = QLineEdit()
l3 = QLabel("Wait (sec):")
wait = QLineEdit()
l2 = QLabel("Iteration:")
l2.move(20,100)
global add1
count = QLineEdit()
fbox = QFormLayout()
fbox.addRow(l1, url)
fbox.addRow(l3, wait)
vbox = QVBoxLayout()
vbox.addWidget(count)
fbox.addRow(l2, vbox)
startButton=QPushButton("Start")
fbox.addRow(startButton)
startButton.clicked.connect(self.requests)
win.setLayout(fbox)
win.setWindowTitle("--")
win.resize(300,200)
win.show()
sys.exit(app.exec_())
def requests(self):
for x in range(0,int(count.text())):
//certain event happens here, which will cause other window to get open
self.dialog = PopUp(self)
self.dialog.show()
def stop(self):
sys.exit()
class PopUp(QMainWindow):
def __init__(self, parent=None):
super(PopUp, self).__init__(parent)
app = QApplication(sys.argv)
win = QWidget()
l1 = QLabel("URL:")
nextUrl = QLineEdit()
fbox = QFormLayout()
fbox.addRow(l1, url)
startButton = QPushButton("Go")
fbox.addRow(startButton)
startButton.clicked.connect(self.requests)
win.setLayout(fbox)
win.setWindowTitle("--")
win.resize(300, 200)
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
I see that code is right but I am getting an unknown error:
QCoreApplication::exec: The event loop is already running
I have searched on google and here in stack overflow but didn't get anything worthy. Anyone knows that why this error comes and why is it coming in my code??
Each PyQt5 application must create one application object app = QApplication(sys.argv). Remove from the class MainWindow and the class PopUp - app = QApplication (sys.argv) .
sys.exit(app.exec_()) - the mainloop of the application, he must also be alone. Remove from the class MainWindow and the class PopUp - sys.exit(app.exec_())
I improved the readability of your example a bit.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class PopUp(QMainWindow):
def __init__(self, x, url, parent=None):
super(PopUp, self).__init__(parent)
self.url = url.text()
self.x = x + 1
self.setWindowTitle("-- PopUp {} --".format(self.x))
self.setGeometry(self.x*100, self.x*100, 300, 200)
win = QWidget()
self.setCentralWidget(win)
nextUrl = QLineEdit(self.url)
self.startButton = QPushButton("Go {}".format(self.x))
fbox = QFormLayout(win)
fbox.addRow(QLabel("URL:"), nextUrl)
fbox.addRow(self.startButton)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.dialogs = []
self.flag = False
win = QWidget()
self.setCentralWidget(win)
self.url = QLineEdit() # + self
wait = QLineEdit()
self.count = QLineEdit() # + self
startButton = QPushButton("Start")
startButton.clicked.connect(self.requests)
fbox = QFormLayout(win)
fbox.addRow(QLabel("URL:"), self.url)
fbox.addRow(QLabel("Wait (sec):"), wait)
fbox.addRow(QLabel("Iteration:"), self.count)
fbox.addRow(startButton)
def requests(self):
if self.dialogs and self.flag:
_ = [ i.hide() for i in self.dialogs]
self.dialogs = []
for x in range(0, int(self.count.text())):
# certain event happens here, which will cause other window to get open
dialog = PopUp(x, self.url, self)
dialog.startButton.clicked.connect(self.requests2)
dialog.show()
self.dialogs.append(dialog)
self.flag = True
def stop(self): # ?
sys.exit()
def requests2(self):
print("def requests2(self): clicked Button {}".format(self.sender().text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.setWindowTitle("-- Title --")
main.resize(300,200)
main.show()
sys.exit(app.exec_())
I'm new to python and pyqt. I'm trying to open a new window after the first screen. My second window opens but without the options I specified, label and pushbutton.
from PyQt5 import QtWidgets
import sys
class secondwindow(QtWidgets.QMainWindow):
def __init__(self):
super(secondwindow, self).__init__()
self.label1 = QtWidgets.QLabel("Second Window");
self.button1 = QtWidgets.QPushButton("Click Me");
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(self.label1)
hbox.addWidget(self.button1)
self.setLayout(hbox)
class Window(QtWidgets.QWidget):
def btnclicked(self):
sender = self.sender()
if sender.text() == "OK":
self.secwin.show()
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.button1 = QtWidgets.QPushButton("OK");
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.button1)
self.setLayout(vbox)
self.button1.clicked.connect(self.btnclicked)
self.secwin = secondwindow()
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
main = Window()
main.show
sys.exit(app.exec())
if __name__ == '__main__':
main()
QMainWindow is a special widget because it has a defined structure, http://doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework:
As shown in the image there is already an area destined to place the widgets called Central Widget, in it you must place the widgets that you want to be displayed for it, you use setCentralWidget().
In your case the solution is:
class secondwindow(QtWidgets.QMainWindow):
def __init__(self):
super(secondwindow, self).__init__()
central_widget = QtWidgets.QWidget()
self.label1 = QtWidgets.QLabel("Second Window")
self.button1 = QtWidgets.QPushButton("Click Me")
hbox = QtWidgets.QHBoxLayout(central_widget)
hbox.addWidget(self.label1)
hbox.addWidget(self.button1)
self.setCentralWidget(central_widget)
I am trying to create a dialog with three tabs using PyQt. However, I am annoyed because although the dialog is displayed, the embedded widgets are not displayed!. I suppose this is a very simple problem with a correspondingly very simple solution, but I am struck! Can anyone give a hint? Thanks in advance!
Here is my code so far:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TabbedDialog(QDialog):
def __init__(self, parent = None):
super(TabbedDialog, self).__init__(parent)
self.tabWidget = QTabWidget()
self.tabWidget.tab1 = QWidget()
self.tabWidget.tab2 = QWidget()
self.tabWidget.tab3 = QWidget()
self.tabWidget.addTab(self.tabWidget.tab1,"Tab 1")
self.tabWidget.addTab(self.tabWidget.tab2,"Tab 2")
self.tabWidget.addTab(self.tabWidget.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("tab demo")
def tab1UI(self):
layout = QFormLayout()
layout.addRow("Name",QLineEdit())
layout.addRow("Address",QLineEdit())
self.tabWidget.setTabText(0,"Contact Details")
self.tabWidget.tab1.setLayout(layout)
def tab2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton("Male"))
sex.addWidget(QRadioButton("Female"))
layout.addRow(QLabel("Sex"),sex)
layout.addRow("Date of Birth",QLineEdit())
self.tabWidget.setTabText(1,"Personal Details")
self.tabWidget.tab2.setLayout(layout)
def tab3UI(self):
layout = QHBoxLayout()
layout.addWidget(QLabel("subjects"))
layout.addWidget(QCheckBox("Physics"))
layout.addWidget(QCheckBox("Maths"))
self.tabWidget.setTabText(2,"Education Details")
self.tabWidget.tab3.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
form = TabbedDialog()
retval = form.exec_()
here is my solution to the problem
On the init method, I declared a layout, then added the 'tabWidget' widget to that layout and set that layout as the layout of your QDialog.
def __init__(self, parent = None):
super(TabbedDialog, self).__init__(parent)
self.tabWidget = QTabWidget()
self.tabWidget.tab1 = QWidget()
self.tabWidget.tab2 = QWidget()
self.tabWidget.tab3 = QWidget()
self.tabWidget.addTab(self.tabWidget.tab1,"Tab 1")
self.tabWidget.addTab(self.tabWidget.tab2,"Tab 2")
self.tabWidget.addTab(self.tabWidget.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("tab demo")
# Here is the addition to the code.
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.tabWidget)
self.setLayout(mainLayout)
i am building a desktop application using PyQt python which has a QwebBrowser. now i am running some function using javascript which is returning a value say abc as per following example.
class QWebView(QWebView):
def contextMenuEvent(self,event):
menu = QMenu()
self.actionShowXpath = menu.addAction("Show Xpath")
QObject.connect(self.actionShowXpath,SIGNAL("triggered()"),self,SLOT("slotshowxpath()"))
menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))
#pyqtSlot()
def slotshowxpath(self):
frame.evaluateJavaScript("var abc = function get()");
result = frame.evaluateJavaScript("abc").toString()
**some code code to put result in QLineEdit Widget**
# something like below
# xpath.setText(result)
def window():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
web = QWebView(w)
web.load(QUrl("http://www.indiatimes.com/"))
web.show()
xpath = QtGui.QLineEdit("", w)
sys.exit(app.exec_())
if __name__ == '__main__':
window()
now, i want to put the value of abc in a QLineEdit widget("xpath") present in my application.please give me suggestion that how i can i do this?
I can't work up an example because QtWebkit has been removed from Qt 5.6, but if the problem you are having is because you don't have a reference to your QLineEdit, then pass the QLineEdit to your QWebView class's __init__() function:
def start_app():
app = QtGui.QApplication(sys.argv)
main_window = QtGui.QWidget()
xpathInput = QtGui.QLineEdit(main_window)
web_view = MyWebView(main_window, xpathInput) #<===HERE
web_view.load(QUrl("http://www.indiatimes.com/"))
main_window.show()
sys.exit(app.exec_())
Then in your QWebView class:
class MyWebView(QWebView):
def __init__(self, parent, xpath_widget):
#QWebView.__init__(parent)
QWebView.__init__(self, parent)
#or: super(MyWebView, self).__init__(parent)
self.xpath_widget = xpath_widget
def contextMenuEvent(self,event):
menu = QMenu()
self.actionShowXpath = menu.addAction("Show Xpath")
#QObject.connect(
# self.actionShowXpath,
# SIGNAL("triggered()"),
# self,SLOT("slotshowxpath()")
#)
self.actionShowXpath.triggered.connect(self.show_xpath)
menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))
##pyqtSlot()
def show_xpath(self):
frame = ...
frame.evaluateJavaScript("var abc = function get()");
result = frame.evaluateJavaScript("abc").toString()
#some code code to put result in QLineEdit Widget**
self.xpath_widget.setText(result)
But I think a better way to organize your code would be to do something like this:
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.xpathInput = QtGui.QLineEdit(self)
self.web_view = QWebView(self)
self.web_view.load(QUrl("http://www.indiatimes.com/"))
self.menu = QMenu()
self.actionShowXpath = self.menu.addAction("Show Xpath")
#QObject.connect(
# self.actionShowXpath,
# SIGNAL("triggered()"),
# self,SLOT("slotshowxpath()")
#)
self.actionShowXpath.triggered.connect(self.show_xpath)
menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))
def show_path(self):
frame = ...
result = frame.evaluateJavaScript("abc").toString()
self.xpathInput.setText(result)
def start_app():
app = QtGui.QApplication(sys.argv)
main_window = MyWindow()
main_window.show()
sys.exit(app.exec_())