0

i am making Gui using qt designer and python.I have two classes on demo.py and demo2.py which are on the same directory/folder.when button clicks on demo,i can show demo2 and hide demo using OOP composition like down below. But then i am so confused why i can't hide demo2(like this down below). This is demo.py

From demo2 import Ui_Form
Class Ui_For(object):
    def log(s):
        s.U=QtGui.QWidget()
        s.x=Ui_Form()
        s.x.setupUi(s.U)
        s.U.show()
        Form.hide()
    def setupUi(self,Form):
        '''the things that are going to be stated here are the Qlabels(QtGui.Qlabel),Qline edit,the button that connects to method log also,which is working good and other things of this window just like the qt designer write it'''
If __name__=='__main__':
    import sys
    app=QtGui.QApplication(sys.argv)
    Form=QtGui.QWidget()
    ui=Ui_For()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

And this is demo2

Class Ui_Form(object):
      def oy(o):
          ui.U.hide()#this the the one bringing error
      def setupUi(self,Form):
          '''And the rest of the code(there is push button here that connects to method oy,and it works good'''

When i go to demo2 widget,when i push a button and connect to method oy,"name 'ui' is not defined" error comes.But why?

Things i tried:

1.def oy(o): o.x=3 print(getattr(o,'x'))#works great and gives me 3 as expected

But if i did this instead,`

print(getattr(ui.x,'x'))#i get "name 'ui' is not defined" error

Why is this?isn't 'o' the same as 'ui.x' in this case?

Tom
  • 100
  • 8
  • I found that if i remove" from demo2 import Ui_Form" and i put the class of demo2 inside the file demo(putting the two classes in one file) ,it works fine(no NameError)!but why?isn't importing the same as putting them together?what is going on here !!? – Tom Feb 08 '19 at 11:56
  • Any code in the `If __name__=='__main__'` block isn't executed when imported as a module. – ekhumoro Feb 08 '19 at 17:54
  • Thanks for ur comment,but thats not the problem here.the if __name__=='__main__' on file demo2 insnt gonna execute for sure but the one in demo will.Then look at 'def log(s)',i made an object for the other class in demo2.Leave "if __name__=='__main__' and just instantiate the object,u will still get name error.Or try accesing method oy from method log of class Ui_For(write ui.x.oy() or s.x.oy()) and it will throw u things on method oy.So yes the If__name__=='__main__' in demo is getting executed.This is not a duplicate of the link u gave me. – Tom Feb 08 '19 at 19:34
  • Thanks again.I read all the answers but here i am using composition.can u tell me Why would i want to do 'import demo' to access ui.U or ui.x in demo2?i made ui.x the object of the class in demo2(oop composition).So why wouldn't i use this object to access things in the class Ui_Form. And as i have mentioned in the bottom,with out importing demo,i can do o.x but not ui.x,why is this?aren't "self" or other first arguments of methods mean the object of the class? – Tom Feb 08 '19 at 20:39
  • Why do you think that a name should be available in a different module when you haven't imported it? Can you really not see that `ui` is defined in the `demo` module, but not in `demo2`? This is very basic python stuff, so I cannot understand what your difficulty is. – ekhumoro Feb 08 '19 at 21:09
  • Yeah you are right.But if i do 'o.x' in the bottom it works and if i do 'ui.x' it doesnt work(i think 'o' in this case is the same as saying 'ui' bcz i read first argument of a method is the place for the object).why is this then?Sorry if its very basic,and for me not understanding u well.I really thank u for being patient with me. – Tom Feb 08 '19 at 21:43
  • The first argument of a method is always `self` (or whatever you choose to name it). So the name `o` is an instance of the class `Ui_Form`, which is defined in the module `demo2`. The name `ui` refers to an instance of the class `Ui_For`, which is defined in the module `demo`. Thus, there is no relationship whatsoever between `ui` and `o`, since they are different instances of different classes defined in different modules. – ekhumoro Feb 08 '19 at 22:13
  • Sorry,i made a typo earlier,i meant "aren't `ui.x` and `o` the same?" Like i said in the question,under 'Things i tried'. – Tom Feb 08 '19 at 23:06
  • That still doesn't explain why you think `ui` should be available in that namespace. Until you answer that, I don't think there's anything more to be said. The error message makes it about as obvious as it can be what the problem is, and I can't make it any plainer myself. It makes no difference whether `ui.x` and `o` are the same object, since that is totally unrelated to the actual error: "name 'ui' is not defined". – ekhumoro Feb 08 '19 at 23:26
  • Ok………this is the thing that i am not getting .if we look in `def log(s): ui.x(s.x)=Ui_Form()`. doesn't that mean x is an attribute of `ui` and `ui.x` is an object of class Ui_Form().so then if i say `ui.x` in demo, its gonna be read like object.attribute but if i say `ui.x.x` in demo2(method oy) i expect it to be read like object.attribute(treating `ui.x` as one) just like when i do `o.x` (read as object.attribute).Refering to what u said,its reading like first `ui` and then `x` but why is it not reading `ui.x` as a single variable or "thing" just like its reading `o` as a single? – Tom Feb 09 '19 at 00:22
  • Because `ui` is not defined. Please work through some basic tutorials. I have nothing further to add. – ekhumoro Feb 09 '19 at 00:26
  • Ok,thanks man for trying to make me understand.can u remove the [duplicate]since this is not related to what u linked. – Tom Feb 09 '19 at 01:03

0 Answers0