Pass argument from qlineedit - python

I want to pass the text from the QLineEdit to the next function.
I want person_name to receive the text i entered in QLineEdit.
I tried using the function add_function(self,people_folder,shape), but when I declared in main at final it says that self is not defined.
What should I do, to make it work for person_name to receive the text I entered in the QlineEdit?

Try to instead of
def add_person(people_folder, shape):
person_name= (sys.argv[0])
adding self parameter to add_person method and calling text() from self.lineEdit instead of sys.argv[0]
def add_person(self, people_folder, shape):
person_name = self.lineEdit.text()
when i declared in main at final it says that self is not defined.
this is due to self is referencing to Object (created Class), you are not supposed to add self in method add_person(self,PEOPLE_FOLDER, SHAPE) when calling it. It's added automatically when called, but you need to call from created Object, like
myCamera = camera()
myCamera.add_person(PEOPLE_FOLDER, SHAPE)

Related

Python Function that creates class instances

Hello i want to create a function which creates instances of a class
def make_instance(name_instance)
name_instance=puppy()
class puppy:
def __init__(self,name):
self.name =name
make_instance(cloud)
# when i pass an argument it says the variable is undefined and i use #isinstance() it return False.
Your puppy class needs to take a name value into its constructor, and you're currently not passing in anything.
Also your function doesn't return the instance at all. It simply re-assigns the instance to the variable name_instance that you pass in (losing your input). The return value of make_instance right now is None
My guess is that you want your implementation to look like the following
def make_instance(name_instance)
return puppy(name_instance)
I do want to point out though that this function isn't useful unless it does more than just create the instance, you're just adding wrapper code around the constructor

When to use self and when not to use self

Which class option is preferable and why?
Option 1
class Person():
def __init__(self):
pass
def sayHello(self, name):
print "{} says hello!".format(name)
def driver(self):
for name in names: # names is generated in the driver function by some means of input
self.sayHello(name)
Option 2
class Person():
def __init__(self):
self.name = None
def sayHello(self):
print "{} says hello!".format(self.name)
def driver(self):
for name in names: # names is generated in the driver function by some means of input
self.name = name
self.sayHello()
You can assume that there are more variables than just name and that multiple functions are using these variables. The main point I am trying to make is that the variable value's are changing inside the for loop
Even though your exemple is syntaxically correct, it doesn't help at all understand your question regarding how to use a instance attribute.
From want I'm guessing, there's two questions :
When to use a class method (def foo(self, bar)) ?
When to use a instance attribute (self.name) ?
Instance attribute should be used when you need to "share" a variable between functions or retrieve it from outside a function. That variable will be "attached" to the object (for exemple, the color of a car, the nickname of a user, ...)
If your function / method need to call this kind of variable, it must use self to get it, so you have to set it as the first argument when defining this function.
If you just need a temporary variable to loop over it and do some stuff, you don't need to use a class method, a simple function will do the trick.

Python: method of a self-made class doesn't accept a tuple as argument

For an assignment I got from school we need to make a chess game. One task is that we need to make a class called chessboard with a couple of methods like place, delete and replace.
My chessboard is a dictionary with the keys as place, in the form of a tuple, and the value as the chess-piece as value. But if I want to give a tuple as argument to my methods it fails.
This is my code:
class ChessBoard:
def __init__(self):
DICT={ (A,1):None,(A,2):None,(A,3):None,(A,4):None,(A,5):None,(A,6):None,(A,7):None,(A,8):None,
(B,1):None,(B,2):None,(B,3):None,(B,4):None,(B,5):None,(B,6):None,(B,7):None,(B,8):None,
(C,1):None,(C,2):None,(C,3):None,(C,4):None,(C,5):None,(C,6):None,(C,7):None,(C,8):None,
(D,1):None,(D,2):None,(D,3):None,(D,4):None,(D,5):None,(D,6):None,(D,7):None,(D,8):None,
(E,1):None,(E,2):None,(E,3):None,(E,4):None,(E,5):None,(E,6):None,(E,7):None,(E,8):None,
(F,1):None,(F,2):None,(F,3):None,(F,4):None,(F,5):None,(F,6):None,(F,7):None,(F,8):None,
(G,1):None,(G,2):None,(G,3):None,(G,4):None,(G,5):None,(G,6):None,(G,7):None,(G,8):None,
(H,1):None,(H,2):None,(H,3):None,(H,4):None,(H,5):None,(H,6):None,(H,7):None,(H,8):None }
def place(self, piece,(row,column)):
self.piece=piece
self.(row,column)=(row,column)
DICT[(row,column)]=self.piece
You are not creating an instance attribute out of your DICT attribute in your init. To set it as an instance attribute you need to add it to your instance as self.DICT
So, your init should looking something like this:
# not showing your full dictionary:
def __init__(self):
self.DICT={ (A,1):None,(A,2):None}
Now, in your other instance methods, you simply access accordingly, as an instance attribute. You are placing a piece at a location, so you seem like you are wanting to set self.piece at row,column. Furthermore, I don't know how your other code looks like, but if your piece argument you are passing in to your place method is only being used in that method, you do not need to set it as an instance attribute.
So, to fix your problem, you can do this:
def place(self, piece,(row,column)):
self.piece=piece
self.DICT[(row,column)]=self.piece
If you in fact are not using piece anywhere else in your code, don't set it as an instance attribute, and you can simply do:
def place(self, piece,(row,column)):
self.DICT[(row,column)] = piece
I think this is what you are trying to achieve,
def place(self, piece, row_column_tuple):
self.piece=piece
self.row_column_tuple=row_column_tuple
DICT[row_column_tuple]=self.piece
Then elsewhere in the code you can call this method like
place(piece, (row, column))

Calling a method with an event handler

Why, if I put a "normal" call to the method button1Click() in the bind call, does my program not even start? By removing the parenthesis the problem is solved.
I'm using this program as reference: Thinking in Tkinter
Also, why should I add the event argument to my button1Click() method?
from Tkinter import *
class MyApp:
def __init__(self, parent):
self.myParent = parent
self.myContainer1 = Frame(parent)
self.myContainer1.pack()
self.button1 = Button(self.myContainer1)
self.button1.configure(text="OK", background= "green")
self.button1.pack(side=LEFT)
self.button1.bind("<Button-1>", self.button1Click) # <--- no () !
def button1Click(self, event):
self.button2 = Button(self.myContainer1, text="lol")
self.button2.bind("<Button-1>", self.button1Click)
self.button2.pack()
root = Tk()
myapp = MyApp(root)
root.mainloop()
bind() expects something that is callable and that expects an argument.
If you pass self.button1Click(), you effectively pass it None, because that is what is returned by this call.
As the call is to be performed by the clickable object, you are not supposed to call it yourself.
So, next step: You pass it self.button1Click, and you clock the button. Then the given "thing" is tried to be called with an event object as argument, but that fails, because the method is not prepared for that.
So you have 2 options:
Either you modify the method so it can be called with an event object, such as def button1Click(self, event):,
or you wrap it in a lambda call: lambda event: self.button1Click().
In the latter case, you give the bind() method a callable which accepts exactly one argument, and does the call as wanted at the time of calling (thus the ()).
You can call the method button1Click "normally" using lambda. What might be happening right now is that it would be getting called anyhow.
For ex: command=lambda:self.button1Click()
You can pass more arguments if you like by putting them in the parenthesis.
You need to use the event argument because whenever you bind a method you are passing event object too automatically. In your case,its two arguments-the event object & self.

Call Module in Other Class

So I have a close() method that is called when the user clicks the close button. The close method is as follows:
def close(self):
ThreadedClient.endApplication()
root.destroy()
The close() method is inside a GUI() class. The close method needs to call the endApplication() method in the ThreadedClient class. But instead it gives me this error:
TypeError: unbound method endApplication() must be called with ThreadedClient instance as first argument (got nothing instead)
This may be a simple solution, but I just don't know how to fix it. Any help is appreciated!
The question you need to be asking is which ThreadedClient needs to have .endApplication() called on it.
Once you have a reference to it (assuming you store a reference in self)
def close(self):
self.threaded_client.endApplication()
# ...
Apparently, endApplication() is an instance method, but ThreadedClient is a class. You need to provide it with the actual instance of ThreadedClient that you want to end.
For instance, if somewhere else you created a threaded client with...
foo = ThreadedClient()
then later on you'd need to call...
foo.endApplication()

Categories

Resources