closing dialog box also closes main window [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I created a dialog box & connected it to main window.Dialog purpose is to assign values to variables when buttons are pressed.My problem is that when i try to close dialog box it also closes the main window.This is code from main window file.
from dialog import Ui_Dialog as Form
This following is the function used to open dialog box
def open_dialog(self):
ap = QtWidgets.QApplication(sys.argv)
ap.setStyle('fusion')
dialog = QtWidgets.QDialog()
dialog.ui = Form()
dialog.ui.setupUi(dialog)
dialog.exec_()
I tried to fix it with help of google.I added (accept/reject) buttonbox of pyqt5 but that also close the main window with dialog box

Do not create new QApplication object in your code.Your code should be like this
def open_dialog(self):
dialog = QtWidgets.QDialog()
dialog.ui = Form()
dialog.ui.setupUi(dialog)
dialog.exec_()
remove this from code
ap = QtWidgets.QApplication(sys.argv)

Related

Change Button Image after a couple seconds (Tkinter) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
How would you code a button, which changes its Image after you press it, but after 3 Seconds it changes Back to its normal state ?
I managed to change the Button Image with a Click, but the function sleep() stops the whole Tkinter Mainloop, which leads to the Button not doing anything.
As mentioned above, you need to make another function which changes the image back. You would then schedule that function to run later using the after method. This would be an ideal place to use a Button subclass to make a custom type of Button that incorporates those functions and the image data. Like this:
import tkinter as tk
class EleckTroniiKz(tk.Button):
"""A new type of Button that shows one image, changes to another image
when clicked, and changes back to the original image after 3 seconds"""
def __init__(self, master=None, image1=None, image2=None, **kwargs):
self.command = kwargs.pop('command', None)
super().__init__(master, **kwargs)
self.image1 = tk.PhotoImage(file=image1)
self.image2 = tk.PhotoImage(file=image2)
self.config(image=self.image1, command=self.on_click)
def on_click(self):
self.config(image=self.image2)
if self.command: self.command()
# schedule the after_click function to run 3 seconds from now
self.after(3000, self.after_click)
def after_click(self):
self.config(image=self.image1)
### test / demo
def main():
root = tk.Tk()
btn = EleckTroniiKz(root, 'OFF.gif', 'ON.gif')
btn.pack()
root.mainloop()
if __name__ == '__main__':
main()

how to run children UI in python PyQt5? [duplicate]

This question already has answers here:
A QApplication instance already exists
(3 answers)
Closed 3 years ago.
I have main ui, and want to run children ui on click in main ui.
children:
class sessionGenerator(QtWidgets.QMainWindow, designTg.Ui_MainWindow2):
def __init__(self):
super().__init__()
self.setupUi(self)
self.showHelp.clicked.connect(self.showHelpFull)
self.next_1.clicked.connect(self.nextOne)
self.next_2.clicked.connect(self.nextTwo)
self.next_3.clicked.connect(self.nextThree)
# all another code
if I use
app2 = QtWidgets.QApplication(sys.argv)
window2 = sessionGenerator()
window2.show()
app2.exec_()
it appears on a second and throws 0xC0000005
just sessionGenerator runs great itself. But i can't figure out how to run it from another ui
Do not create second main application.
only
window2 = sessionGenerator()
window2.show()
This lines need to be before first app exec.
app.exec_()

QListWidget synchronized using common datasource [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a main window with a mdiArea in it. In the area there are 2 subwindows, and possibly more. I want to have everyone have a QListWidget that shares the information and they are synchronized. I searched but I can not find any help.
I was thinking of something like a static list, I want to share the data between all the sub-windows and allow anyone to work on them, 1 window at a time
A widget can only have one parent and it was drawn in the window that belongs to the parent, so the answer to your direct question is that it can not. But I think that in the background you want several views to share the same data and be synchronized, if so, the solution is to use a proxy so that several models share the same data at all times.
In the case of the QListWidget you can not set a model but it will be the one used as a base and the widgets copies will be QListView.
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
mdiarea = QtWidgets.QMdiArea()
self.setCentralWidget(mdiarea)
list_widget = QtWidgets.QListWidget()
for i in range(10):
it = QtWidgets.QListWidgetItem('item {}'.format(i))
it.setFlags(it.flags() | QtCore.Qt.ItemIsEditable)
list_widget.addItem(it)
sub_window = QtWidgets.QMdiSubWindow()
sub_window.setWidget(list_widget)
mdiarea.addSubWindow(sub_window)
for _ in range(4):
list_view = self.create_qlistview(list_widget.model())
sub_window = QtWidgets.QMdiSubWindow()
sub_window.setWidget(list_view)
mdiarea.addSubWindow(sub_window)
def create_qlistview(self, model):
proxy = QtCore.QIdentityProxyModel()
proxy.setSourceModel(model)
list_view = QtWidgets.QListView()
list_view.setModel(proxy)
return list_view
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())

How to remove minimize button in PySide [duplicate]

This question already has answers here:
Qt hide minimize, maximize and close buttons
(6 answers)
Closed 7 years ago.
How can I remove the minimize button from QMainWindow in Python?
I'm using Python 3.4 with PySide.
thanks
You need to utilize setWindowFlags do prevent the minimize and maximize button from appearing. You'll have to set the appropriate flags as well.
In this case, you need to enable CustomizeWindowHint and then disable both WindowMinimizeButtonHint and WindowMaximizeButtonHint (alternatively, you could just disable WindowMinMaxButtonsHint, which handles the previous two flags).
A very simple program demonstrating how this works:
import sys
from PySide import QtGui
from PySide import QtCore
def main():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.setWindowFlags(w.windowFlags() & QtCore.Qt.CustomizeWindowHint)
w.setWindowFlags(w.windowFlags() & ~QtCore.Qt.WindowMinMaxButtonsHint)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Outputs:

How to add new label in window? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a window. There is a button. When the user clicks on this button, in the window must disappear button and appear new label and new button.
Do you understand?
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
def starting():
quest1 = QtGui.QWidget()
quest1.setWindowTitle('New')
quest1.resize(900, 600)
quest1.show()
quest1.exec()
testing = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setWindowTitle('Title')
window.resize(900, 600)
MainText = QtGui.QLabel('<p align="center"; style="font-size: xx-large">Text</p>')
Mainbox = QtGui.QVBoxLayout()
# buttons
start = QtGui.QPushButton('Start')
quit = QtGui.QPushButton('Exit')
start.setFixedSize(70, 40)
quit.setFixedSize(70, 40)
buttons = QtGui.QHBoxLayout()
buttons.addWidget(start)
buttons.addWidget(quit)
# /buttons
Mainbox.addWidget(MainText)
Mainbox.addLayout(buttons)
window.setLayout(Mainbox)
QtCore.QObject.connect(quit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT("quit()"))
QtCore.QObject.connect(start, QtCore.SIGNAL('clicked()'), starting)
window.show()
sys.exit(testing.exec_())
I think, I do wrong, is not required to make new window, but I don`t know what I must doing.
Qt UIs are built from widgets. Many widgets can have children. If you add/remove children, the UI will update accordingly.
The problem with the code above is that you don't add the new widget to a parent. So what happens is: You create the widget, you force it to appear, the function ends, the local variables (newwindow) end up on the trash and Python cleans the trash -> the widget is deleted again.
For the window to stay, you need to add it to some parent widget (probably the window). If you want to replace existing widgets, you need to remove them yourself.

Categories

Resources