QListWidget synchronized using common datasource [closed] - python

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())

Related

How to show message in popup window while python script is running [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 1 year ago.
Improve this question
Is there any way to show the message in popup window that process is running when python script start and disappear the popup window when process is done. Thanks in advance
A very flexible method you could look into is creating a tkinter window:
https://docs.python.org/3/library/tkinter.html
import tkinter as tk
import time
def some_function():
print('Do stuff')
time.sleep(3)
if __name__ == '__main__':
# Create window
window = tk.Tk()
# Create label
label_var = tk.StringVar()
label_var.set('Program is running...')
label = tk.Label(window, textvariable=label_var)
label.pack()
# Update and show window once
window.update_idletasks()
window.update()
# Your function code
some_function()
# Get rid of window
window.destroy()
Edit: just saw someone answered with tkinter in the comments... will leave this here as an example

closing dialog box also closes main window [closed]

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)

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 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.

Simple PyQt demo from book doesn't work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm brand new to GUI programming under Python and just got the book "Rapid GUI Programming with Python and QT" by Summerfield. The very first simple example ("pop-up alert in 25 lines") on page 112 works, but my attempt to exactly replicate the second example ("an expression evaluator in 30 lines") on page 116 produces only a blank window, with no visible fields for either entry or output and not even a window title. This is under Mac OS X 10.8.5 using the latest Enthought Canopy 64-bit Python installation (1.2.0.1610)
The complete contents of PyQtdemo.pyw is
from __future__ import division
import sys
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Form(QDialog):
def _init__(self, parent=None):
super(Form,self).__init__(parent)
self.browser = QTextBrowser()
self.lineedit = QLineEdit("Type an expression and press Enter")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(layout)
self.lineedit.setFocus()
self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text = unicode(self.lineedit.text())
self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
except:
self.browser.append("<font color=red>%s is invalid!</font>" % text)
app = QApplication(sys.argv)
print dir(app)
form = Form()
form.show()
app.exec_()
It seems to me there are only the following possibilities:
there's a typo in my code that I've overlooked;
there's something wrong with how I'm invoking the script (e.g., "python PyQtdemo.pyw");
there's something wrong with my PyQt 4.10.3-1 installation;
there's an error in the book.
It's a typo in your code.
The Form.__init__ method is missing an initial underscore, and so it never gets called.
(PS: This also explains why the incorrect indentation of the updateUi method doesn't raise an AttributeError when it's referenced in self.connect).

Categories

Resources