The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Persistent "import os"?
Discuss Persistent "import os"? in the Python Programming forum on Dev Shed. Persistent "import os"? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 11th, 2003, 01:19 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Time spent in forums: 10 m 28 sec
Reputation 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.
|

June 11th, 2003, 11:15 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Time spent in forums: 10 m 28 sec
Reputation 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.
|

June 12th, 2003, 10:35 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Time spent in forums: 10 m 28 sec
Reputation 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.
|

June 15th, 2003, 04:37 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
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 
|

June 16th, 2003, 07:26 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Time spent in forums: 10 m 28 sec
Reputation 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.
|

June 17th, 2003, 04:01 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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
|

June 18th, 2003, 11:10 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 9
Time spent in forums: 10 m 28 sec
Reputation 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 
|

June 20th, 2003, 07:52 AM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
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.
|

June 21st, 2003, 04:15 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

June 21st, 2003, 05:56 AM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
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).
|

June 21st, 2003, 08:01 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|