function inputs in classes - python

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.

Related

easygui buttonbox crashes becase of default font from Tkinter

I'm using easygui in a Python app and then generate an .exe with PyInstaller.
Everything works fine in my computer, but my colleague get this weird error when they try to run the app :
Traceback (most recent call last):
File "easygui\boxes\button_box.py", line 95, in buttonbox
File "easygui\boxes\button_box.py", line 147, in __init__
File "easygui\boxes\button_box.py", line 268, in __init__
File "tkinter\font.py", line 23, in nametofont
File "tkinter\font.py", line 86, in __init__
RuntimeError: main thread is not in main loop
The line where easygui is called is simply
choice = easygui.buttonbox(
"msg", "title", choices=["choice1", "choice2"],
default_choice="choice1", cancel_choice="choice2"
)
so the problem seems to be with the font but I'm not using anything particular in easygui ? I've searched for issues on the easygui's Git but couldn't find anything
Also, there was another easygui.buttonbox earlier in the process but this one did show up properly so I'm just really confused.
Thanks!
The solution was to show the msgbox in the main thread. It crashed because the msgbx was in a different thread than the main one

Did pyglet change recently?

So, The other day, I went back to an old project to finish it but when I tried running it, it gave me several errors that I've tried to fix but some of them I don't know how to fix. The program worked fine when I stopped working on it, so I'm very confused at what happened. My guess is that pyglet got a new update and removed a lot of stuff I used to use. Some of the errors I get are as follows
Traceback (most recent call last):
File "C:\Users\____\Desktop\Code\3D\Projects\3D Program 1\main.py", line 9,
in <module>
screen =
pyglet.window.get_platform().get_default_display().get_default_screen()
AttributeError: module 'pyglet.window' has no attribute 'get_platform'
and
Traceback (most recent call last):
File "C:\Users\____\Desktop\Code\3D\Projects\3D Program 1\main.py", line 216, in <module>
main()
File "C:\Users\____\Desktop\Code\3D\Projects\3D Program 1\main.py", line 208, in main
win = Window(caption = "3D Program 1", width = WINW, height = WINH, resizable = False, vsync = True)
File "C:\Users\____\Desktop\Code\3D\Projects\3D Program 1\main.py", line 57, in __init__
self.tex1 = get_tex("steel_tex.png")
File "C:\Users\____\Desktop\Code\3D\Projects\3D Program 1\main.py", line 21, in get_tex
tex = pyglet.image.load(file).texture
AttributeError: 'ImageData' object has no attribute 'texture'
I also had problems with it saying that there was no such thing as the "pyglet.graphics.TextureGroup" class
Yes, it looks like both of snippets of code you show are using features that were deprecated in previous versions of Pyglet and removed in the recent 1.4 release. You can see the deprecation (and their suggested replacements) in the documentation for older versions:
The get_platform() function in pyglet.window:
Deprecated. Use pyglet.canvas.Display.
The .texture attribute of an image:
Deprecated. Use get_texture().

python bind not working

I have the basic screensaver program. I want to bind Mouse button 1 to delete everything on screen, but when I press the Button-1 I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
TypeError: delete() takes 0 positional arguments but 1 was given
.
.
.
.
This is my program:
import tkinter
canvas=tkinter.Canvas(width=1000,height=800)
canvas.pack()
from random import *
entry1=tkinter.Entry()
entry1.pack()
def setric():
x=randrange(900)
y=randrange(700)
global a
canvas.delete('all')
farba=choice(("red","purple","blue","lightblue","green","lightgreen"))
if a>-90:
a=a-10
else:
a=-10
if entry1.get()=="":
canvas.update()
canvas.after(3000,setric)
a=0
else:
canvas.create_text(x,y,text=entry1.get(),font="Arial 40",fill=farba,angle=a)
canvas.after(2000, setric)
def delete():
canvas.delete("all")
a=0
setric()
canvas.bind('<Button-1>',delete)
Even when I change what is in the def delete I still get the error.
Thanks for your time! I am new to this so I have no idea where is the problem
....................................................................................................................................................................................................................................................
Functions called via bindings are automatically passed an object that represents the event. Your delete method needs to be able to accept this parameter even if you don't use it.
def delete(event):
canvas.delete("all")

PYQT4 NameError global name 'ui' error

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

Exception raised with trace_variable()

I have a function that is supposed to build a window according to which option in a dropdown menu is selected:
def buildview():
value = StringVar()
options = ["one", "two", "three"]
menu = OptionMenu(*(root, value) + tuple(options))
### Some window building accoring to the value selected... ###
value.trace_variable("w", buildview)
The exception that is raised looks like this (EDIT: Entire Traceback):
Traceback (most recent call last):
File "D:\Dropbox\PRO\infograbber\Infograbber 0.1.py", line 102, in <module>
mainloop()
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 405, in mainloop
_default_root.tk.mainloop(n)
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1553, in __call__
self.widget._report_exception()
AttributeError: 'StringVar' object has no attribute '_report_exception'
What exactly is causing this? Can I not have a method call back itself like this? I don't even know where to start fixing this problem, so I'd appreciate any help.
I'm using Python 3.5 64 bit, Sublime Text 2, Windows 10.
EDIT:
Added a test callback function:
def test(*args):
print("test")
and changed the above trace to
value.trace_variable("w", test)
Now the exception changed to this:
Traceback (most recent call last):
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "D:\Dropbox\PRO\infograbber\Infograbber 0.1.py", line 56, in buildview
root.trace_variable("w", self.printcurrentarticle)
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1948, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'trace_variable'
I'm not entirely sure if this is the only problem, but it's definitely a problem. When a trace fires it passes in three arguments. The function you've defined takes no arguments. You need to pass in a reference to a function that takes at least three arguments.
You also have the problem that each time the trace fires, you are creating another variable and another trace. On the surface that seems like a bad idea, unless you really do want to create a new option menu every time an optionmenu changes.

Categories

Resources