User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 403,366 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,337 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1025 | Replies: 16
Reply
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Calling Shell Commands

  #1  
Mar 7th, 2008
Alright, I'm trying to make a small GUI application that'll allow me to run a .ck file (with ChucK) from a GUI, from a preset directory.

Okay, so I have:

  1. from Tkinter import *
  2.  
  3. class chuckrun:
  4.  
  5. def __init__(self, master):
  6.  
  7. ui = Frame(master)
  8. ui.pack()
  9.  
  10. self.file = Entry(ui, text="Filename")
  11. self.file.pack(side=LEFT)
  12.  
  13. self.play = Button(ui, text="Play", command=playCk)
  14. self.play.pack(side=RIGHT)
  15.  
  16. def playCk(self):
  17. p = os.popen('chuck')
  18. # run chuck $HOME/Apps/Source/ChucK/Filename
  19. # (from the Entry field above)
  20.  
  21. root = Tk()
  22.  
  23. app = chuckrun(root)
  24.  
  25. root.mainloop()

In the def playCk(self) part, I have no idea what to do. What I want it to do is to run the command chuck $HOME/Apps/Source/ChucK/filename where filename is the filename specified in the Entry field above (as stated in the comment).

But, what I really need it to do is store the text in the entry field into a variable (x), then call the shell command chuck $HOME/Apps/Source/ChucK/x. But I have no idea how to use variables, say from Python to the shell and vice versa.

Can anyone help me?

Thank you in advance!
Last edited by linux : Mar 7th, 2008 at 9:57 pm.
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 408
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 56
woooee woooee is offline Offline
Posting Pro in Training

Re: Calling Shell Commands

  #2  
Mar 7th, 2008
You will likely want to use a Tkinter StringVar() with the .get() to get the contents of the entry box. If memory serves - a Google for "Tkinter StringVar Entry" will yield thousands of hits as well
var = Tkinter.StringVar()
entry_box = Tkinter.Entry(frame_name, width=5, textvariable=var)
python_var=var.get() [Under def playCk(self): for your app]
http://infohost.nmt.edu/tcc/help/pub...variables.html
You can run the command using os.system() or subprocess.
os.system("chuck $HOME/Apps/Source/ChucK/" + python_var)
Last edited by woooee : Mar 7th, 2008 at 10:57 pm.
Reply With Quote  
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Calling Shell Commands

  #3  
Mar 7th, 2008
os.system("chuck $HOME/Apps/Source/ChucK/" + python_var)

EXACTLY what I wanted. Thank you so much.
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote  
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Calling Shell Commands

  #4  
Mar 7th, 2008
Solved, but not totally:

alex@alex-laptop:~/Apps/Source/Python Source$ python chuckrun.py 
Exception in Tkinter callback
Traceback (most recent call last):
  File "lib-tk/Tkinter.py", line 1406, in __call__
    return self.func(*args)
  File "chuckrun.py", line 11, in playCk
    os.system("chuck $HOME/Apps/Source/ChucK/" + filename)
TypeError: cannot concatenate 'str' and 'instance' objects

It shows the GUI, and I input foo.ck, and I get that error.

My full code for this is:
  1. from Tkinter import *
  2. import os
  3.  
  4. class chuckrun:
  5.  
  6. def __init__(self, master):
  7. filename = StringVar()
  8.  
  9. def playCk():
  10.  
  11. os.system("chuck $HOME/Apps/Source/ChucK/" + filename)
  12.  
  13. ui = Frame(master)
  14. ui.pack()
  15.  
  16. self.file = Entry(ui, text="Filename", textvariable=filename)
  17. self.file.pack(side=LEFT)
  18.  
  19. self.play = Button(ui, text="Play", command=playCk)
  20. self.play.pack(side=RIGHT)
  21.  
  22.  
  23. root = Tk()
  24.  
  25. app = chuckrun(root)
  26.  
  27. root.mainloop()

EDIT: By the way, will:

  1. def stopCk():
  2.  
  3. os.system("^C")

Actually kill the shell command?
Last edited by linux : Mar 7th, 2008 at 11:36 pm.
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote  
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Calling Shell Commands

  #5  
Mar 8th, 2008
  1. from Tkinter import *
  2. import os
  3.  
  4. class chuckrun:
  5.  
  6. def __init__(self, master):
  7. filename=StringVar()
  8.  
  9. def playCk():
  10.  
  11. os.system("chuck " + str(filename))
  12.  
  13. ui = Frame(master)
  14. ui.pack()
  15.  
  16. # Text entry for gathering filename
  17. self.file = Entry(ui)
  18. self.file.pack(side=LEFT)
  19.  
  20. # Play button for executing the file
  21. self.play = Button(ui, text="Play", command=playCk)
  22. self.play.pack(side=LEFT)
  23.  
  24. # Kills the application
  25. self.close = Button(ui, text="Close", command=ui.quit)
  26. self.close.pack(side=LEFT)
  27.  
  28. root = Tk()
  29. root.title("ChucK")
  30.  
  31. app = chuckrun(root)
  32.  
  33. root.mainloop()

I have that so far. However, it seems to be completely ignoring whatever the user puts in the textbox. What am I doing wrong?
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote  
Join Date: Dec 2006
Posts: 408
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 56
woooee woooee is offline Offline
Posting Pro in Training

Re: Calling Shell Commands

  #6  
Mar 8th, 2008
Python variables are different from Tkinter variables. You have to use .get
def playCk():
     python_var=filename.get()
     os.system("chuck " + python_var)
Reply With Quote  
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Calling Shell Commands

  #7  
Mar 8th, 2008
alex@alex-laptop:~/Apps/Source/Python Source$ python chuckrun.py 
  File "chuckrun.py", line 9
    def playCk():
    ^
IndentationError: unexpected indent
alex@alex-laptop:~/Apps/Source/Python Source$ 

  1. #!/usr/bin/python/
  2. from Tkinter import *
  3. import os
  4.  
  5. class chuckrun:
  6.  
  7. def __init__(self, master):
  8. filename=StringVar()
  9. def playCk():
  10.  
  11. python_var=filename.get()
  12. os.system("chuck " + python_var)
  13.  
  14. ui = Frame(master)
  15. ui.pack()
  16.  
  17. # Text entry for gathering filename
  18. self.file = Entry(ui)
  19. self.file.pack(side=LEFT)
  20.  
  21. # Play button for executing the file
  22. self.play = Button(ui, text="Play", command=playCk())
  23. self.play.pack(side=LEFT)
  24.  
  25. # Kills the application
  26. self.close = Button(ui, text="Close", command=ui.quit)
  27. self.close.pack(side=LEFT)
  28.  
  29. root = Tk()
  30. root.title("ChucK")
  31.  
  32. app = chuckrun(root)
  33.  
  34. root.mainloop()

That error doesn't seem logical to me. My indent seems to be in the right place, isn't it?
Last edited by linux : Mar 8th, 2008 at 1:17 pm.
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote  
Join Date: Dec 2006
Posts: 408
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 56
woooee woooee is offline Offline
Posting Pro in Training

Re: Calling Shell Commands

  #8  
Mar 8th, 2008
We all have programs that are out to get us. This one is yours. See anything unusual here
    def __init__(self, master):
    	filename=StringVar()
		def playCk():
When you can't find a problem on the line of the error, look at the previous line as well.
Last edited by woooee : Mar 8th, 2008 at 2:29 pm.
Reply With Quote  
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Calling Shell Commands

  #9  
Mar 8th, 2008
I actually indented the filename=StringVar() over one more, and it executes fine. However, it issues the chuck command before the user enters the information and says chuck: No input files. How can I make it wait?
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote  
Join Date: Aug 2006
Location: me::house
Posts: 908
Reputation: linux is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 23
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Calling Shell Commands

  #10  
Mar 11th, 2008
Sorry for the bump.
But, bump!

Can anyone help?
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 9:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC