PYQT4 NameError global name 'ui' error - python

I am a newbie pyqt4 learner and i am a facing a problem while linking .py files with each other
def sigwin(self):
self.signUpWindow = QtGui.QDialog()
self.ui = Ui_Dialog2()
self.ui.setupUi(self.signUpWindow)
self.signUpWindow.show()
this is coding of my main.py file which is a window with a sign up button when i click on sign up it gives me error like this
Traceback (most recent call last):
File "main.py", line 40, in sigwin
self.ui.setupUi(self.signUpWindow)
File "/root/folder/abc/sign.py", line 80, in setupUi
a.clicked.connect(ui.signwin)
NameError: global name 'ui' is not defined
my main file is linked with sign.py file on which i think its giving me problem on the function which is linked with another file
def signwin(self):
self.welcomeWindow = QtGui.QDialog()
self.ui = Ui_Dialog1()
self.ui.setupUi(self.welcomeWindow)
self.welcomeWindow.show()
a=self.pushButton
a.clicked.connect(ui.signwin)
this a=self code is the signup button linked with signwin
its giving me error of global ui idk how to solve this
hope it helps

Related

why I get an error on Thread-1 when running my python program from an .exe file with a Tkinter GUI?

I design a GUI with Tkinter and also I add a function to convert a .docx file to .PDF file with the docx2pdf library.
I execute the main function of my python code in a Thread because when I run my code from the .exe file the GUI is locked (like it freezes).
I add an event to a button to run the main function in a new thread.
import tkinter
from tkinter import ttk
from threading import *
from docx2pdf import convert
def mProcess():
input = "C:\\test.docx"
output = "C:\\file.pdf"
convert(input,output)
def mainTask():
t = Thread(target=mProcess)
t.daemon = True
t.start()
### GUI
win = Tk()
ttk.Button(win, text = 'Start', command = mainTask).pack(pady=20)
win.mainloop()
If I run this code from my VS code it runs well. The problem only happens when I generate the .exe file of my code and run it.
I generate the exe file with pyinstaller test.py --onefile --windowed.
The error I get when run from the exe file:
Exception in thread Thread-1:
Traceback (most recent call last):
File "win32com\client\dynamic.py", line 89, in _GetGoodDispatch
pywintypes.como_error: (-2147221008, 'CoInitialize has not been called.', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "threading.py", line 973, in bootstrap_inner
File "threading.py", line 910, in run

pyqt4 problems with resorce system (TypeError: qRegisterResourceData)

When I run this code (which I created a module for my qrc file)
import sys
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
mainwindow = uic.loadUiType("mainmenu2.ui")[0] #load the UI
class Mainmenu(QtGui.QMainWindow, mainwindow):
def __init__(self, parent=None):
super(Mainmenu, self).__init__(parent)
self.setupUi(self)
#self.aboutp.clicked.connect(self.linkabout)
app = QtGui.QApplication(sys.argv)
mainwindow = Mainmenu
#aboutwindow = About_us_page(None)
mainwindow.show()
app.exec_()
I get this error
Traceback (most recent call last): File "C:\Users\megan\Desktop\newqt\try1.py", line 7, in <module>
mainwindow = uic.loadUiType("mainmenu2.ui")[0] #load the UI File "C:\Python34\lib\site-packages\PyQt4\uic\__init__.py", line 211, in loadUiType
exec(code_string.getvalue(), ui_globals) File "<string>", line 202, in <module> File "C:\Users\megan\Desktop\newqt\test_rc.py", line 49680, in <module>
qInitResources() File "C:\Users\megan\Desktop\newqt\test_rc.py", line 49675, in qInitResources
QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data) TypeError: qRegisterResourceData(int, str, str, str): argument 2 has unexpected type 'str`'
so I know the problem is in my resource file, and I've looked online and the only help I could find was this comment
pyrcc generates Python 2.x code by default.
Try regenerating your resource files using pyrcc with flag '-py3'
But I don't know how to do that so please if someone could help me solve this problem, thanks
Using powershell or the cmd go to the directory where the resource file is stored and run:
`pyrcc4 resource.qrc -o resource_rc.py -py3`

Tkinter : Can't get register function to work [duplicate]

I've looked around a bit, but I can't find an answer to my error. Here is the code:
import tkinter as tk
root=tk.Tk()
class Page(tk.Frame):
'''Enables switching between pages of a window.'''
def __init__(self):
self.widgets={}
self.grid(column=0,row=0)
page=Page()
tk.mainloop()
Here is the error:
Traceback (most recent call last):
File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line 11, in <module>
page=Page()
File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line , in __init__
self.grid(column=0,row=0)
File "C:\Python34\lib\tkinter\__init__.py", line 2055, in grid_configure
self.tk.call(
AttributeError: 'Page' object has no attribute 'tk'
I'm fairly new to tkinter, and this error has me stumped. I'd really appreciate any help, thank you!
Your Page init method should call Frame's init.
class Page(tk.Frame):
'''Enables switching between pages of a window.'''
def __init__(self):
super(Page, self).__init__()
self.widgets={}
self.grid(column=0,row=0)

function inputs in classes

So i have started using classes in python and i am still learning how it works. so in this part of my code and i found something that i don't understand.
def Button(self):
self.Button = QPushButton("Push me Bitch!",self)
self.Button.setGeometry(QtCore.QRect(500, 500, 5, 5))
self.Button.resize(self.Button.minimumSizeHint())
self.Button.clicked.connect(self.ButtonPress(someInput))
# Do functions
def ButtonPress(self , someInput):
print('someInput')
self.OutputBox1.setText('I Has Been Pressed!')
And this is the error that i got:
Traceback (most recent call last):
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 92, in <module>
ex = Example()
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 40, in __init__
self.Button()
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 68, in Button
self.Button.clicked.connect(self.ButtonPress(5))
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 74, in ButtonPress
self.OutputBox1.setText('I Have Been Pressed!!!')
AttributeError: 'Example' object has no attribute 'OutputBox1'
If i remove the someInput. Then there is no problem. How does this work?
Best regards
Your event setup is wrong, when you attach an event to a button you generally want to link the function, not the result of the function to the clicked event.
Instead of:
self.Button.clicked.connect(self.ButtonPress(someInput))
You want:
self.Button.clicked.connect(self.ButtonPress)
That way the function will get called when the button is clicked.
You might have some other issues with your code, but start by fixing that.

Name Error: name 'QFileDialog' is not defined

I am working on coursework for computer science and can't work out why the piece of code isn't working. I am trying to connect a button that I've created in PyQt4 so that when it is pressed it shows a directory dialogue:
self.Browse_Button_1 = QtGui.QToolButton(self.tab)
self.Browse_Button_1.setGeometry(QtCore.QRect(360, 30, 61, 20))
self.Browse_Button_1.setObjectName(_fromUtf8("Browse_Button_1"))
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
self.Browse_Button_1.clicked.connect(self, file)
However every time I run the program I just get this error:
Traceback (most recent call last):
File "D:\NEA Project\NEA_UI.py", line 194, in <module>
ui = Ui_Dialog()
File "D:\NEA Project\NEA_UI.py", line 30, in __init__
self.setupUi(self)
File "D:\NEA Project\NEA_UI.py", line 55, in setupUi
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
NameError: name 'QFileDialog' is not defined
Any help with the problem would be greatly appreciated.
QFileDialog is in the QtGui module, so you need to append that to beginning of your line, e.g.:
file = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
Alternatively, if you want to use QFileDialog without the QtGui in front, you need to import it from the module (at the top of your file), with:
from PyQt4.QtGui import QFileDialog
Or for Qt5 (note that in Qt5, QFileDialog moved to the QtWidgets module):
from PyQt5.QtWidgets import QFileDialog

Categories

Resources