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:

from Tkinter import *

class chuckrun:

    def __init__(self, master):

        ui = Frame(master)
        ui.pack()

	self.file = Entry(ui, text="Filename")
	self.file.pack(side=LEFT)

        self.play = Button(ui, text="Play", command=playCk)
        self.play.pack(side=RIGHT)

	def playCk(self):
		p = os.popen('chuck')
		# run chuck $HOME/Apps/Source/ChucK/Filename
		# (from the Entry field above)

root = Tk()

app = chuckrun(root)

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/[I]filename[/I] 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!

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/pubs/tkinter/control-variables.html
You can run the command using os.system() or subprocess.
os.system("chuck $HOME/Apps/Source/ChucK/" + python_var)

os.system("chuck $HOME/Apps/Source/ChucK/" + python_var) EXACTLY what I wanted. Thank you so much.

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:

from Tkinter import *
import os

class chuckrun:

    def __init__(self, master):
		filename = StringVar()
		
		def playCk():
			
			os.system("chuck $HOME/Apps/Source/ChucK/" + filename)
			
		ui = Frame(master)
		ui.pack()

		self.file = Entry(ui, text="Filename", textvariable=filename)
		self.file.pack(side=LEFT)

		self.play = Button(ui, text="Play", command=playCk)
		self.play.pack(side=RIGHT)

		
root = Tk()

app = chuckrun(root)

root.mainloop()

EDIT: By the way, will:

def stopCk():

	os.system("^C")

Actually kill the shell command?

from Tkinter import *
import os

class chuckrun:
	
    def __init__(self, master):
		filename=StringVar()
		
		def playCk():
			
			os.system("chuck " + str(filename))
			
		ui = Frame(master)
		ui.pack()

		# Text entry for gathering filename
		self.file = Entry(ui)
		self.file.pack(side=LEFT)

		# Play button for executing the file
		self.play = Button(ui, text="Play", command=playCk)
		self.play.pack(side=LEFT)
		
		# Kills the application
		self.close = Button(ui, text="Close", command=ui.quit)
		self.close.pack(side=LEFT)
		
root = Tk()
root.title("ChucK")

app = chuckrun(root)

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?

Python variables are different from Tkinter variables. You have to use .get

def playCk():
     python_var=filename.get()
     os.system("chuck " + python_var)
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$
#!/usr/bin/python/
from Tkinter import *
import os

class chuckrun:
	
    def __init__(self, master):
    	filename=StringVar()
		def playCk():
			
			python_var=filename.get()
			os.system("chuck " + python_var)
		
		ui = Frame(master)
		ui.pack()

		# Text entry for gathering filename
		self.file = Entry(ui)
		self.file.pack(side=LEFT)

		# Play button for executing the file
		self.play = Button(ui, text="Play", command=playCk())
		self.play.pack(side=LEFT)
		
		# Kills the application
		self.close = Button(ui, text="Close", command=ui.quit)
		self.close.pack(side=LEFT)
			
root = Tk()
root.title("ChucK")

app = chuckrun(root)

root.mainloop()

That error doesn't seem logical to me. My indent seems to be in the right place, isn't it?

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.

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?

Sorry for the bump.
But, bump!

Can anyone help?

Here is something I had lying around. It's not all that good but does what you want

class EntryDemo :

    def __init__( self ) :
        self.top = Tkinter.Tk()

        self.text=""

        self.text_tk = Tkinter.StringVar()
        label1 = Tkinter.Label(self.top, text = "Enter Text" )
        label1.pack()

        frame_s = Tkinter.Frame( self.top )
        label_s = Tkinter.Label( frame_s, text = "Text " )
        label_s.pack( side = "left" )
        entry_s = Tkinter.Entry(frame_s, width=5, textvariable=self.text_tk)
        entry_s.pack( side = "right")
        frame_s.pack()
      
        cont = Tkinter.Button(self.top, text='PRINT',
               command=self.get_text, bg='blue', fg='white' )
        cont.pack(fill=Tkinter.X, expand=1)

        exit=  Tkinter.Button(self.top, text='FORGET IT',
               command=self.top.quit, bg='red', fg='white' )
        exit.pack(fill=Tkinter.X, expand=1)

        self.top.mainloop()

    def get_text( self ) :
        self.text = self.text_tk.get()
        print "text entered was", self.text

##----------------------------------------------------------------------
if __name__ == "__main__":
   ED=EntryDemo()
   print "Class's text =", ED.text
alex@alex-laptop:~/Apps/Source/Python Source$ python chuckrun.py 
Traceback (most recent call last):
  File "chuckrun.py", line 39, in <module>
    app = chuckrun(root)
  File "chuckrun.py", line 24, in __init__
    self.play = Button(ui, text="Play", command=playCk())
UnboundLocalError: local variable 'playCk' referenced before assignment
alex@alex-laptop:~/Apps/Source/Python Source$

My code is:

#!/usr/bin/python/
from Tkinter import *
import os

class chuckrun:
	
    def __init__(self, master):
    		filename=StringVar()
		
		'''
		def playCk():
			
			python_var=filename.get()
			os.system("chuck " + python_var)
		'''
		ui = Frame(master)
		ui.pack()

		# Text entry for gathering filename
		self.file = Entry(ui)
		self.file.pack(side=LEFT)

		# Play button for executing the file
		self.play = Button(ui, text="Play", command=playCk())
		self.play.pack(side=LEFT)
		
		# Kills the application
		self.close = Button(ui, text="Close", command=ui.quit)
		self.close.pack(side=LEFT)
			
		def playCk():
			
			python_var=filename.get()
			os.system("chuck " + python_var)
			
root = Tk()
root.title("ChucK")

app = chuckrun(root)

root.mainloop()

The ''' part is to place where the function was before. But... Why is it giving me the error that I can't call it? I realize it's below when I call the function, but how can I make it see it beforehand?

The class can not find playCk, should be
def playCk(self):
and the command should be
command=self.playCk()
If you really do want to define playCk outside of the class, then the def playCk() et al must come before the class.

My code:

#!/usr/bin/python/
from Tkinter import *
import os

class chuckrun:
	
    def __init__(self, master):
    		filename=StringVar()
		'''		
		def playCk(self):
			
			python_var=filename.get()
			os.system("chuck " + python_var)
		'''
		ui = Frame(master)
		ui.pack()

		# Text entry for gathering filename
		self.file = Entry(ui)
		self.file.pack(side=LEFT)

		# Play button for executing the file
		self.play = Button(ui, text="Play", command=self.playCk())
		self.play.pack(side=LEFT)
		
		# Kills the application
		self.close = Button(ui, text="Close", command=ui.quit)
		self.close.pack(side=LEFT)
		
		def playCk(self):
			
			python_var=filename.get()
			os.system("chuck " + python_var)
			
root = Tk()
root.title("ChucK")

app = chuckrun(root)

root.mainloop()

Error:

Traceback (most recent call last):
  File "chuckrun.py", line 38, in <module>
    app = chuckrun(root)
  File "chuckrun.py", line 23, in __init__
    self.play = Button(ui, text="Play", command=self.playCk())
AttributeError: chuckrun instance has no attribute 'playCk'

----

My code:

#!/usr/bin/python/
from Tkinter import *
import os

class chuckrun:
	
    def __init__(self, master):
    		filename=StringVar()
		
		def playCk(self):
			
			python_var=filename.get()
			os.system("chuck " + python_var)
		
		ui = Frame(master)
		ui.pack()

		# Text entry for gathering filename
		self.file = Entry(ui)
		self.file.pack(side=LEFT)

		# Play button for executing the file
		self.play = Button(ui, text="Play", command=self.playCk())
		self.play.pack(side=LEFT)
		
		# Kills the application
		self.close = Button(ui, text="Close", command=ui.quit)
		self.close.pack(side=LEFT)
		'''
		def playCk(self):
			
			python_var=filename.get()
			os.system("chuck " + python_var)
		'''
root = Tk()
root.title("ChucK")

app = chuckrun(root)

root.mainloop()

Same error.

Did I understand your suggestion?

You have def playCk(self): indented incorrectly again. It should be at the same level as __init__

class EntryDemo :

    def __init__( self ) :
        self.top = Tkinter.Tk()
        self.top.geometry("225x100+10+10" )
        self.text=""

        self.text_tk = Tkinter.StringVar()
        label1 = Tkinter.Label(self.top, text = "Enter Filename" )
        label1.pack()

        frame_s = Tkinter.Frame( self.top )
        label_s = Tkinter.Label( frame_s, text = "Text " )
        label_s.pack( side = "left" )
        entry_s = Tkinter.Entry(frame_s, width=25, textvariable=self.text_tk)
        entry_s.pack( side = "right")
        frame_s.pack()
      
        cont = Tkinter.Button(self.top, text='Play File',
               command=self.get_text, bg='blue', fg='white' )
        cont.pack(fill=Tkinter.X, expand=1)

        exit=  Tkinter.Button(self.top, text='FORGET IT',
               command=self.top.quit, bg='red', fg='white' )
        exit.pack(fill=Tkinter.X, expand=1)

        entry_s.focus_set()
        self.top.mainloop()

    def get_text( self ) :
        self.text = self.text_tk.get()
        print "get_text --> text entered was", self.text
        ##os.system( "chuck " + self.text )     ## you could also play from here
        self.top.destroy()

##----------------------------------------------------------------------
if __name__ == "__main__":
   ED=EntryDemo()
   print "Class's text =", ED.text
   os.system( "chuck " + ED.text )
File "chuckrun.py", line 32
    def playCk(self):
                    ^
IndentationError: unindent does not match any outer indentation level

Hmm...

Bumpppppp. Can someone help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.