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

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

Related

Why is MyApp Instance not executing on python run [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 4 months ago.
Improve this question
enter image description hereI'm writing a simple code for executing an application in python kivy module I use Pycharm, the syntax is okay but its returning an error saying:
File "C:\Users\USER\PycharmProjects\pythonProject7\main.py", line 12, in <module>
TestNas().run()
TypeError: TestNas() missing 1 required positional argument: 'app'
I was expecting it to start the simple Kivy Gui but it did not.
import kivy
from kivy.app import App
from kivy.uix.label import Label
def TestNas(app):
def build(self):
return Label(Text="Test this app")
if __name__ == '__main__':
TestNas().run()
First of all TestNas() should be a class and I'm guessing it needs to be a subclass of App() that you imported. So change TestNas() to a class and capitalize App().
I think this is the code you need:
import kivy
from kivy.app import App
from kivy.uix.label import Label
class TestNas(App):
def build(self):
return Label(Text="Test this app")
if __name__ == '__main__':
TestNas().run()

Python - Having some troubles with the super function and importing [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 8 months ago.
Improve this question
I have been trying to transition to Python recently but my brain isn't working well with it.
I really can't find what is the problem.
window.py
import tkinter as tk
class Window(tk.Tk):
def __init__(self):
super(Window, self).__init__()
main.py
from window import Window
win = Window()
win.mainloop()
Error message:
File "main.py", line 3, in <module>
win = Window()
File "window.py", line 5, in __init__
super(Window, self).__init__()
TypeError: super() argument 1 must be type, not classobj
Am I just being dumb?
Thank you.
If you are using a recent python version super itself needs no arguments:
class Window(tk.Tk):
def __init__(self):
super().__init__()
I am so sorry everybody ran the file with python 2.
Just typed "python3 main.py" rather than "python main.py"

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

Python 3.7.0 and PyQt5.11.2: module 'PyQt5.uic' has no attribute 'loadUI' [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 4 years ago.
Improve this question
I am using the following code trying to show my Qt window:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUI('app.ui', self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_)
Unfortunately, I always receive the error
AttributeError: module 'PyQt5.uic' has no attribute 'loadUI'
I have already checked whether I accidentally used Qt4 attributes, but could not find any.
It should be load.Ui, not load.UI.

error in label python, editor pycharm version [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 6 years ago.
Improve this question
I have this:
__author__ = 'User'
from kivy.app import App
from kivy.uix.button import Label
class Hello2(App):
def build(self):
return Label()
if __name__=="__main__":
Hello2().run()
But I get an error on this line:
return Label()
And I dont know why it is an error
I have it now like this:
__author__ = 'User'
from kivy.app import App
from kivy.uix.button import Label
class Hello2(App):
def build(self):
return Label()
if __name__=="__main__":
Hello2().run()
but still the same error
Your indentation is wrong. Try reindenting like this:
__author__ = 'User'
from kivy.app import App
from kivy.uix.button import Label
class Hello2(App):
def build(self):
return Label()
if __name__=="__main__":
Hello2().run()

Categories

Resources