Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 11th, 2003, 01:19 PM
red_over_blue red_over_blue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 red_over_blue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old June 11th, 2003, 11:15 PM
red_over_blue red_over_blue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 red_over_blue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #3  
Old June 12th, 2003, 10:35 PM
red_over_blue red_over_blue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 red_over_blue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old June 15th, 2003, 04:37 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 513 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old June 16th, 2003, 07:26 PM
red_over_blue red_over_blue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 red_over_blue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old June 17th, 2003, 04:01 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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

Reply With Quote
  #7  
Old June 18th, 2003, 11:10 AM
red_over_blue red_over_blue is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 red_over_blue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old June 20th, 2003, 07:52 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 513 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old June 21st, 2003, 04:15 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #10  
Old June 21st, 2003, 05:56 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 513 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).

Reply With Quote
  #11  
Old June 21st, 2003, 08:01 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Persistent "import os"?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap