How to re Open my closed subwindow in PyQt5 QStackWidget? - python

Add My second File In QStackwidget.
After pressing the "close" Button from the second file, I can't able to reopen it.
How to reopen the second file? How to refresh the code?
How to close the second/Subwindow file properly and re-open the same from the first file. or If we close the second file, How to refresh the first file fully.
First File
import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from sample_countrypage import Countrypage
class MainPage(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle(" Sample Programme")
self.setGeometry(100,100,1000,600)
self.Ui()
self.show()
def Ui(self):
self.countrywindow = Countrypage()
self.stackitems = QStackedWidget()
self.btn1=QPushButton("Open 2nd File")
self.btn1.setFixedSize(100, 30)
self.btn1.clicked.connect(self.countrypage)
self.left_layout = QVBoxLayout()
self.right_layout = QHBoxLayout()
self.main_layout = QHBoxLayout()
self.left_layout.addWidget(self.btn1)
self.left_layout.addStretch()
self.right_frame = QFrame()
self.right_layout = QHBoxLayout(self.right_frame)
self.main_layout.setSpacing(5)
self.main_layout.setContentsMargins(0,0,0,0)
self.main_layout.addLayout(self.left_layout)
self.main_layout.addWidget(self.right_frame)
self.main_layout.addStretch()
self.stackitems.addWidget(self.countrywindow)
self.right_layout.addWidget(self.stackitems)
widget = QWidget()
widget.setLayout(self.main_layout)
self.setCentralWidget(widget)
def countrypage(self):
self.stackitems.setCurrentWidget(self.countrywindow)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainPage()
app.setStyle("fusion")
mainwindow.show()
sys.exit(app.exec_())
SecondFile
import sys,os
from PyQt5.QtWidgets import *
class Countrypage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Country Page")
self.btn1 = QPushButton("close")
self.btn1.clicked.connect(lambda : self.close())
self.form_layout = QFormLayout()
self.form_layout.addRow("Country",QLineEdit())
self.form_layout.addRow("continent",QLineEdit())
self.layout_btn = QHBoxLayout()
self.layout_btn.addStretch()
self.layout_btn.addWidget(self.btn1)
self.layout_country = QVBoxLayout()
self.layout_country.addLayout(self.form_layout)
self.layout_country.addLayout(self.layout_btn)
self.layout_country.addStretch()
self.setLayout(self.layout_country)
if __name__=="__main__":
app = QApplication(sys.argv)
countrywin = Countrypage()
countrywin.show()
sys.exit(app.exec_())

If you have hidden it then you have to call show():
def countrypage(self):
self.countrywindow.show()
self.stackitems.setCurrentWidget(self.countrywindow)
Note: It is advisable to avoid the abuse of lambda methods, if they are not strictly necessary then do not use them, in your case using a lambda to invoke the close method is unnecessary so you should use:
self.btn1.clicked.connect(self.close)
In addition, the concept of files only serves to order the project but it does not make sense in the execution of the program since the same logic applies if all the logic is in the same file.

Related

How to open a new MDI sub-window in PyQt5?

What I want to do is to open a new Countrypage sub-window by clicking on the "New" button which is in Countrypage itself.
For example, if I click the "New" button in a CountryPage window (window title: "Country page"), one more new Countrypage window will be opened in the MDI area (window title: "Country Page 1"). Now if we click the "New" button in "Country Page 1", one more new window will open in the MDI area (window title: "Country page 2") and so on - and I want to close the windows one by one by pressing the corresponding "Close" button in Countrypage. New window are opened only by pressing a "New" button.
And if we close the last opened window by pressing the "Close" button, the text item in the "Country" text-box will be automatically updated in the previous window's "Country" text-box and so on.
Main Script :
import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from sample_countrypage import Countrypage
class MainPage(QMainWindow):
count = 0
def __init__(self):
super().__init__()
self.mdi = QMdiArea()
self.mdi.setFixedSize(1000,400)
self.mdi.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.mdi.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setWindowTitle(" Sample Programme")
self.setGeometry(100,100,1600,600)
self.Ui()
self.show()
def Ui(self):
self.btn1=QPushButton("Country")
self.btn1.setFixedSize(100, 30)
self.btn1.clicked.connect(self.countrypage)
self.left_layout = QVBoxLayout()
self.right_layout = QHBoxLayout()
self.main_layout = QHBoxLayout()
self.left_layout.setContentsMargins(3,5,5,3)
self.left_layout.addWidget(self.btn1)
self.left_layout.addStretch()
self.right_layout.addWidget(self.mdi)
self.main_layout.setSpacing(5)
self.main_layout.setContentsMargins(0,0,0,0)
self.main_layout.addLayout(self.left_layout)
self.main_layout.addLayout(self.right_layout)
self.main_layout.addStretch()
widget = QWidget()
widget.setLayout(self.main_layout)
self.setCentralWidget(widget)
self.subwindow1 = QMdiSubWindow()
self.subwindow1.setObjectName("SubWindow_1")
# self.subwindow1.setWindowFlag(Qt.FramelessWindowHint)
print(Countrypage.btn2click)
def countrypage(self):
self.countrywindow = Countrypage()
self.subwindow1.setWidget(self.countrywindow)
self.subwindow1.setWindowTitle("Create Country")
self.subwindow1.setFixedWidth(300)
self.mdi.addSubWindow(self.subwindow1)
self.subwindow1.show()
self.mdi.cascadeSubWindows()
self.countrywindow.closeRequsted.connect(self.subwindow1close)
def subwindow1close(self):
print("close activated from mdi programme")
self.subwindow1.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainPage()
app.setStyle("Windows")
mainwindow.show()
sys.exit(app.exec_())
Countrypage.py
import sys,os
from PyQt5.QtWidgets import QWidget,QApplication,QPushButton,QLineEdit,QFormLayout,QVBoxLayout,QHBoxLayout
from PyQt5.QtCore import pyqtSignal
class Countrypage(QWidget):
closeRequsted = pyqtSignal()
def __init__(self):
super().__init__()
self.btn1 = QPushButton("close")
self.btn2 = QPushButton("New")
self.btn1.clicked.connect(self.result)
self.btn2.clicked.connect(self.btn2click)
self.tb_country = QLineEdit()
self.tb_continent =QLineEdit()
self.form_layout = QFormLayout()
self.form_layout.addRow("Country",self.tb_country)
self.form_layout.addRow("continent",self.tb_continent)
self.form_layout.addRow("",self.btn2)
self.form_layout.addRow("",self.btn1)
self.setLayout(self.form_layout)
def result(self):
self.closeRequsted.emit()
def btn2click(self):
btn2text = (self.btn2.text())
print(btn2text)
if __name__=="__main__":
app = QApplication(sys.argv)
countrywin = Countrypage()
countrywin.show()
sys.exit(app.exec_())
The adding and closing of sub-windows is best handled by the main-window. The CountryPage class doesn't need to know anything about the sub-windows. The new/close buttons can be directly connected to methods of the main-window. This makes it easier to manage the sub-windows via the functions of the mdi-area.
Below is a re-write of your example which should do what you asked for:
Main Script:
import sys, os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MainPage(QMainWindow):
def __init__(self):
super().__init__()
self.mdi = QMdiArea()
self.mdi.setFixedSize(1000, 400)
self.mdi.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.mdi.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setWindowTitle("Sample Programme")
self.setGeometry(100, 100, 1600, 600)
self.Ui()
def Ui(self):
self.btn1 = QPushButton("Country")
self.btn1.setFixedSize(100, 30)
self.btn1.clicked.connect(self.countrypage)
self.left_layout = QVBoxLayout()
self.right_layout = QHBoxLayout()
self.main_layout = QHBoxLayout()
self.left_layout.setContentsMargins(3, 5, 5, 3)
self.left_layout.addWidget(self.btn1)
self.left_layout.addStretch()
self.right_layout.addWidget(self.mdi)
self.main_layout.setSpacing(5)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout.addLayout(self.left_layout)
self.main_layout.addLayout(self.right_layout)
self.main_layout.addStretch()
widget = QWidget()
widget.setLayout(self.main_layout)
self.setCentralWidget(widget)
def countrypage(self):
page = Countrypage()
subwindow = self.mdi.addSubWindow(page)
subwindow.setWindowTitle("Create Country")
subwindow.setFixedWidth(300)
page.btn_close.clicked.connect(self.subwindowclose)
page.btn_new.clicked.connect(self.countrypage)
subwindow.show()
self.mdi.cascadeSubWindows()
def subwindowclose(self):
print("close activated from mdi programme")
current = self.mdi.activeSubWindow()
if current is not None:
self.mdi.activatePreviousSubWindow()
previous = self.mdi.activeSubWindow()
if previous is not None:
previous.widget().update_fields(current.widget())
current.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainPage()
app.setStyle("Windows")
mainwindow.show()
sys.exit(app.exec_())
Countrypage.py:
import sys,os
from PyQt5.QtWidgets import QWidget,QApplication,QPushButton,QLineEdit,QFormLayout,QVBoxLayout,QHBoxLayout
from PyQt5.QtCore import pyqtSignal
class Countrypage(QWidget):
def __init__(self):
super().__init__()
self.btn_close = QPushButton("Close")
self.btn_new = QPushButton("New")
self.tb_country = QLineEdit()
self.tb_continent = QLineEdit()
self.form_layout = QFormLayout()
self.form_layout.addRow("Country", self.tb_country)
self.form_layout.addRow("Continent", self.tb_continent)
self.form_layout.addRow("", self.btn_close)
self.form_layout.addRow("", self.btn_new)
self.setLayout(self.form_layout)
def update_fields(self, other):
if isinstance(other, Countrypage):
self.tb_country.setText(other.tb_country.text())
self.tb_continent.setText(other.tb_continent.text())
else:
raise TypeError('invalid page type')

How to make automatically save text in QTextEdit?

How can I make QTextEdit save automatically when I'm done typing? I do not want it to be saved automatically when writing each letter separately. Rather, it is saved after all writing is completed only.
import sys
from PyQt5.QtWidgets import *
class MyMainWindow(QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
layout = QHBoxLayout()
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
self.textedit = QTextEdit()
self.textedit.textChanged.connect(self.save_text)
layout.addWidget(self.textedit)
def save_text(self):
text=self.textedit.toPlainText()
with open('mytextfile.txt', 'w') as f:
f.write(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = MyMainWindow()
form.show()
sys.exit(app.exec_())
The trick might be to create a button, once the button is pressed then save the text:
import sys
from PyQt5.QtWidgets import *
class MyMainWindow(QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
layout = QHBoxLayout()
centralWidget = QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
self.textedit = QTextEdit()
layout.addWidget(self.textedit)
self.pushbutton = QPushButton()
self.pushbutton.clicked.connect(self.save_text)
layout.addWidget(self.textedit)
def save_text(self):
text=self.textedit.toPlainText()
with open('mytextfile.txt', 'w') as f:
f.write(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = MyMainWindow()
form.show()
sys.exit(app.exec_())
Then once you done writing your text, just click the button and it will save your text.

change label from other class in a different file

I am creating an application where I have a main window whit a label and then a docked widget that is in another file. I want to change the main windows label from a button at the docked widget. I try to import the main window file but then I can not access to the label. And I also tried to call a function in the main windows that changes the label but then the label does not change.
Here is the code:
main_window.py:
import results_window
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
# Define de Main window properties
self.setMinimumSize(QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
self.top = 50
self.left = 0
self.width = 1300
self.height = 400
self.setGeometry(self.left, self.top, self.width, self.height)
self.result = QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
# Create dock widgets
self.results_window = results_window.results_window()
self.resultsWindowDock = QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
results_window.py:
import main_window
class results_window(QWidget):
def __init__(self):
super(results_window, self).__init__()
print("init")
self.label = QLabel()
self.value = QLineEdit()
self.bt = QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
self.main_layout = QVBoxLayout()
self.main_layout.addWidget(self.label)
self.main_layout.addWidget(self.value)
self.main_layout.addWidget(self.bt)
self.setLayout(self.main_layout)
def clickMethod(self):
print(self.value.text())
text = self.value.text()
main_window.result.setText(text)
You are using the wrong tools, for example your code has a circular import that causes your application to close since it is equivalent to a while True.
In Qt, signals and slots are used to share data asynchronously, as well as contributing to the fact that there is no coupling between classes. In your case, Results_Window must have a signal that transmits that information to the MainWindow, this signal must be emit within clickMethod.
results_window.py
from PyQt5 import QtCore, QtWidgets
class Results_Window(QtWidgets.QWidget):
resultChanged = QtCore.pyqtSignal(str)
def __init__(self):
super(Results_Window, self).__init__()
print("init")
self.label = QtWidgets.QLabel()
self.value = QtWidgets.QLineEdit()
self.bt = QtWidgets.QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addWidget(self.label)
main_layout.addWidget(self.value)
main_layout.addWidget(self.bt)
#QtCore.pyqtSlot()
def clickMethod(self):
text = self.value.text()
self.resultChanged.emit(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
w = Results_Window()
w.show()
sys.exit(app.exec_())
main_window.py
from PyQt5 import QtCore, QtGui, QtWidgets
import results_window
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
self.setMinimumSize(QtCore.QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
top, left, width, height = 50, 0, 1300, 400
self.setGeometry(left, top, width, height)
self.result = QtWidgets.QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
self.results_window = results_window.Results_Window()
self.results_window.resultChanged.connect(self.result.setText)
self.resultsWindowDock = QtWidgets.QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
I had similar problem in PyQT5 where I was unable to access and set the local variables. After a lot of struggle I found writing to file and reading from file as the best solution. Simply write the desired output to file and access the same info from other file. Works great!

How to show all entered text in line edit to text edit widget in pyqt4

Below is my code. I want to show all entered text in line edit to textedit widget. Whenever i entered a text in line edit the same will show in textedit widget. But it's overwrite with latest one. but i want previous user entered text also in text edit widget.
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.title = QtGui.QLabel("TO DO APP")
self.title.setStyleSheet("font: bold 30ft AGENTORANGE")
self.title.setAlignment(QtCore.Qt.AlignCenter)
# self.title.move(200,10)
self.title.resize(90,50)
self.message_box = QtGui.QLineEdit()
self.btn = QtGui.QPushButton("add")
self.btn.clicked.connect(self.message_Chat)
self.tedit = QtGui.QTextEdit()
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.message_box)
self.hbox.addWidget(self.btn)
# self.btn.move(120,100)
# self.message_box.move(220,100)
self.vbox = QtGui.QVBoxLayout()
self.vbox.addWidget(self.title)
self.vbox.addLayout(self.hbox)
self.vbox.addWidget(self.tedit)
self.setLayout(self.vbox)
self.setWindowTitle("To do app")
self.setGeometry(100,100,500,500)
self.show()
def message_Chat(self):
# print(self.message_box.text())
#print(self.message_box.text())
text = self.message_box.text()
self.tedit.setText(text)
# cursor = self.tedit.textCursor()
# cursor.movePosition(QtGui.QTextCursor.End)
# self.tedit.setTextCursor(cursor)
self.message_box.setText("")
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You have to use the append() method to add the text to the end, on the other hand it is more readable to use clear() to clean the QLineEdit that setText("").
def message_Chat(self):
text = self.message_box.text()
self.tedit.append(text)
self.message_box.clear()

PyQt window closes after launch

I'm trying to use a QPushButton to call a function that opens a new instance of QWebView. Works but as soon as the window opens it closes again.
I've read this - PyQt window closes immediately after opening but I don't understand how to reference the window to keep it open.
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebSettings
from PyQt4.QtNetwork import QNetworkAccessManager
from PyQt4.QtNetwork import *
UA_STRING = """Test Test Test"""
vidurl = ("empty")
def web1():
class YWebPage(QtWebKit.QWebPage):
def __init__(self):
super(QtWebKit.QWebPage, self).__init__()
def userAgentForUrl(self, url):
return UA_STRING
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(800,600) # Viewport size
self.centralwidget = QtGui.QWidget(self)
self.html = QtWebKit.QWebView()
def browse(self):
self.webView = QtWebKit.QWebView()
self.yPage = YWebPage()
self.webView.setPage(self.yPage)
self.webView.load(QtCore.QUrl(vidurl)) # Video URL
self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True) # Enables flash player
self.webView.show()
x = Browser()
# QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.HttpProxy, "proxy.example.com", 8080)) # Proxy setting
x.browse()
def main(): # Dialog window
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(200, 450)
w.setFixedSize(200, 350)
w.move(300, 300)
w.setWindowTitle('U-bot 0.1')
# Setup GUI
# Start Button
w.__button = QtGui.QPushButton(w)
w.__button.clicked.connect(lambda: web1())
# Text area
w.__qle = QtGui.QLineEdit(w)
w.__qle.setText ("http://")
vidurl = w.__qle.text # Get video url from user
# Images
pixmap1 = QtGui.QPixmap("ubot.png")
lbl1 = QtGui.QLabel(w)
lbl1.resize(200, 150)
lbl1.setPixmap(pixmap1)
lbl1.setScaledContents(True)
w.__button.setText('Start')
layout = QtGui.QVBoxLayout()
layout.addStretch(1)
layout.addWidget(w.__qle)
layout.addWidget(w.__button)
w.setLayout(layout)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
app.exec_()
Create a MainWindow class that keeps a list of open Browsers, and every time when you open a browser, just add it to the list. And when a browser window closes, it will remove itself from the list, see closeEvent.
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebSettings
from PyQt4.QtNetwork import QNetworkAccessManager
from PyQt4.QtNetwork import *
UA_STRING = """Test Test Test"""
vidurl = ("empty")
class YWebPage(QtWebKit.QWebPage):
def __init__(self):
super(YWebPage, self).__init__()
def userAgentForUrl(self, url):
return UA_STRING
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self, main, url):
QtGui.QMainWindow.__init__(self)
self.main = main
self.resize(800,600) # Viewport size
self.webView = QtWebKit.QWebView()
self.setCentralWidget(self.webView)
self.yPage = YWebPage()
self.webView.setPage(self.yPage)
self.webView.load(QtCore.QUrl(url)) # Video URL
self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True) # Enables flash player
def closeEvent(self, event):
self.main.browsers.remove(self)
super(Browser, self).closeEvent(event)
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.browsers = []
self.resize(200, 450)
self.setFixedSize(200, 350)
self.move(300, 300)
self.setWindowTitle('U-bot 0.1')
# Setup GUI
# Start Button
self.__button = QtGui.QPushButton('Start')
self.__button.clicked.connect(self.open)
# Text area
self.__qle = QtGui.QLineEdit()
self.__qle.setText("http://")
# Images
pixmap1 = QtGui.QPixmap("ubot.png")
lbl1 = QtGui.QLabel()
lbl1.resize(200, 150)
lbl1.setPixmap(pixmap1)
lbl1.setScaledContents(True)
layout = QtGui.QVBoxLayout()
layout.addStretch(1)
layout.addWidget(self.__qle)
layout.addWidget(self.__button)
self.setLayout(layout)
def open(self):
b = Browser(self, self.__qle.text())
b.show()
self.browsers.append(b)
def main():
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To keep a reference to a QObject, you can either keep the variable in scope, or add it as the child of another QObject which variable already stays in scope.
And for QWidget, the parent should also be a QWidget, so, in your case, you'd want to make w as the parent of all your QMainWindows.
def web1(parent):
...
class Browser(QtGui.QMainWindow): # "Browser" window
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent)
...
def main():
...
w.__button.clicked.connect(lambda: web1(w))
This also avoid maintaining manually a list of opened windows, since the object hierarchy can do that for you.
PS: The child windows are shown as toplevel windows and not inside the w window, because QMainWindow (and QDialog) has the Qt::Window flag set by default.

Categories