#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0

    Persistent "import os"?


    I'm having a problem similar to this one but not exactly. I tried getting help on irc, and was getting great help from "bradb", but I just wasn't getting it.

    I hope I explain this well. I use qt-designer to auto-generate a form. Within the form that is dynamically created are the event handlers for the buttons. However, these handlers must use commands that require "import os". This file is called form1.py and contains a single class called Form1 that defines the form and the event methods.

    Now, since form1.py is dynamically created by designer, it does not have "import os" in it. I wish to create another module that inherits the Form1 class, but also "imports os" so that my methods will work. Say we call this module gui.py

    Finally, a third module will create an instance of the form from gui.py and display it. Call this one pystream.py

    I can get the form to display fine, but when i press any of the buttons, I still get:

    Code:
    Traceback (most recent call last):
      File "/home/john/python/pystream/form1.py", line 82, in pushButton1_4_clicked
        os.popen('killall -9 mplayer')
    NameError: global name 'os' is not defined
    They are short, so here are the files:

    form1.py:
    Code:
    # Form implementation generated from reading ui file 'form1.ui'
    #
    # Created: Wed Jun 11 11:55:54 2003
    #      by: The PyQt User Interface Compiler (pyuic)
    #
    # WARNING! All changes made in this file will be lost!
    
    
    import sys
    from qt import *
    
    
    class Form1(QDialog):
        def __init__(self,parent = None,name = None,modal = 0,fl = 0):
            QDialog.__init__(self,parent,name,modal,fl)
    
            if not name:
                self.setName("Form1")
    
    
    
            self.pushButton1_2 = QPushButton(self,"pushButton1_2")
            self.pushButton1_2.setGeometry(QRect(30,50,91,21))
    
            self.pushButton1_3 = QPushButton(self,"pushButton1_3")
            self.pushButton1_3.setGeometry(QRect(30,80,91,21))
    
            self.pushButton1 = QPushButton(self,"pushButton1")
            self.pushButton1.setGeometry(QRect(30,20,91,21))
    
            self.pushButton5 = QPushButton(self,"pushButton5")
            self.pushButton5.setGeometry(QRect(171,20,60,21))
    
            self.pushButton5_2 = QPushButton(self,"pushButton5_2")
            self.pushButton5_2.setGeometry(QRect(171,50,60,21))
    
            self.pushButton1_4 = QPushButton(self,"pushButton1_4")
            self.pushButton1_4.setGeometry(QRect(140,80,91,21))
            self.pushButton1_4.setPaletteBackgroundColor(QColor(255,0,0))
    
            self.languageChange()
    
            self.resize(QSize(259,114).expandedTo(self.minimumSizeHint()))
    
            self.connect(self.pushButton1,SIGNAL("clicked()"),self.pushButton1_clicked)
            self.connect(self.pushButton1_2,SIGNAL("clicked()"),self.pushButton1_2_clicked)
            self.connect(self.pushButton1_3,SIGNAL("clicked()"),self.pushButton1_3_clicked)
            self.connect(self.pushButton1_4,SIGNAL("clicked()"),self.pushButton1_4_clicked)
            self.connect(self,SIGNAL("destroyed(QObject*)"),self.Form1_destroyed)
    
        def languageChange(self):
            self.setCaption(self.tr("PyStream"))
            self.pushButton1_2.setText(self.tr("977 - 80s"))
            self.pushButton1_3.setText(self.tr("Classical"))
            self.pushButton1.setText(self.tr("Techno"))
            self.pushButton5.setText(self.tr("Vol Up"))
            self.pushButton5_2.setText(self.tr("Vol Dn"))
            self.pushButton1_4.setText(self.tr("Stop"))
    
        def pushButton1_clicked(self):
    
            	os.popen('killall -9 mplayer')
            	os.popen('mplayer -cache 256 /usr/share/media/audio/streams/techno.pls &')
            
    
        def pushButton1_2_clicked(self):
    
            	os.popen('killall -9 mplayer')
            	os.popen('mplayer -cache 256 /usr/share/media/audio/streams/977.pls &')
            	self.Form1.setCaption("PyStream - Playing 977 - 80s")
            
    
        def pushButton1_3_clicked(self):
    
            	os.popen('killall -9 mplayer')
            	os.popen('mplayer -cache 256 /usr/share/media/audio/streams/classical.pls &')
            	self.Form1.setCaption("PyStream - Playing Classical")
            
    
        def pushButton1_4_clicked(self):
    
            	os.popen('killall -9 mplayer')
            	self.Form1.setCaption("PyStream")
            
    
        def Form1_destroyed(self,a0):
            print "Form1.Form1_destroyed(QObject*): Not implemented yet"
    
    
    if __name__ == "__main__":
        a = QApplication(sys.argv)
        QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
        w = Form1()
        a.setMainWidget(w)
        w.show()
        a.exec_loop()
    gui.py:
    Code:
    #!/usr/bin/env python
    
    import sys,os
    import form1
    
    class gui(form1.Form1):
    	pass
    and finally pystream.py:
    Code:
    #!/usr/bin/env python
    
    import sys,qt,os,gui
    
    def main():
    
    	# make the application object (it needs the list of arguments)
    	app = qt.QApplication(sys.argv)
    
    	# ensure the application ends when its last window is closed
    	qt.QObject.connect(app,qt.SIGNAL('lastWindowClosed()'),app,qt.SLOT('quit()'))
    
    	# let's see the form that lives in myform.py
    	form = gui.gui()
    	app.setMainWidget(form)
    	form.show()
    	app.exec_loop()
    	
    
    
    if __name__ == "__main__":
        main()
    I hope I wasn't to verbose including the source code, but I was having trouble explaining what everything did on irc so I thought I might as well. Thanks for any tips or suggestions.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0
    Just an update, thanks again to irc, a suggestion was made to just include the "import os" statement in each event handler.

    I am still not 100% happy about this whole situation. It seems to me that the way I am doing things is rather clumsy. I know it is my fault for not doing it a better way. So, if you are someone who creates your gui apps using PyQt and designer, how to you do it?

    Is there a good tut around specifically dealing with PyQt and designer? I would LOVE to see it. Thanks.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0
    Well, I got this thing working the way I think most people would do it.

    I created a module that contained only the form... no code for event handlers.

    I then created another module that imported the form class, and added the event handlers. This way, the new module takes care of all the functionality, and the class in the form simply defines my GUI.

    I'm sure this is old hat to most of you, but I am just starting GUI programming, and OOP for that matter, and am very happy to be over this initial crux.
  6. #4
  7. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    I've had similar problems... it's about the one annoying thing when programming with PyQt (everything else about it being brilliant)... especially when you start opening dialogues that tie in with code from the main window and other code that isn't UI-specific, the code structure can start to look a little clumsy
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0
    telex4,

    If it wasn't for your quickrip python code, I don't know where I would be. I find it alot easier to learn from looking at actual code, instead of 5 pages of definitions and conventions.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    PyQT


    Hi, very interested in PyQT.. since you both seem to be really into it does anyone know a good tutorial I could read? is PyQT anything like VB's gui designer?

    Thanks,
    Mark
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0
    Unfortunately, I couldn't find a good tutorial for PyQt. That is why I am still not 100% sure that I am using the right framework when designing my applications.

    Also, Telex4 is much more experienced than I am. Once I get a better grip on things I am thinking of making a quick PyQt howto, to at least get someone started.

    I would just suggest reading the Qt documentation to that you understand signals and slots. The better you understand Qt, the easier you will find the python bindings. Sorry this probably isn't what you are looking for, but I am in the same boat you are. I am looking for a "This is the way it should be done HowTo". At this point, I feel more like I am just hacking code to get things to work, but not necessarily doing things the proper way. Also, ignore all my code in my first post, it is crap
  14. #8
  15. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    There isn't, at present, any good tutorials for beginners. There's a good book which you can read without the diagrams online (search google for pyqt book tutorial), but it's quite difficult to learn from (or at least I found it was).

    I've been meaning to write a good tutorial for a long time, having written a lot of GNU/Linux and Pygame documentation, but I've never found the time. That and I still don't feel entirely confident with PyQt... it allows a lot of different ways of doing things, and it's important to learn the correct ways before writing a tutorial.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'll take a look, GUI programming is a big thing but I have to admit that web based app's are a lot more fun, and portable, simply beacuse you don't need anything else installed and because of platform dependencies if only somone would just write a good platform independant GUI that would be great! that's even if it's possible

    Ah well telex4 let me know if you every write this PyQT turtorial, i'm sure it would be very good!

    Tave fun,
    Mark.
  18. #10
  19. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Well, in a way, PyQt, PyGtk and most of the other options are cross-platform. Tk is also bundlded with Python, so that's automatically cross-latform (afaik).
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Well not really, TK wont run on Mac's I know for sure but I'm not sure about the others, but I have my douts. See what would be good would be a GUI package which runs the same on all platforms without having to change your code! PyGame runs the same on all platforms I think, but then thats not really GUI persay, something like that would be brilllll.

    Mark.

IMN logo majestic logo threadwatch logo seochat tools logo