2

I have a button at my window. If I cklick it, I want to start the VLC and stream an URL.

def startstream():
    args = ['C:/Program Files/VideoLAN/VLC/vlc.exe', 'http://dreambox:8001/1:0:19:7B:B:85:C00000:0:0:0:']
    subprocess.call(args)

# Buttons
button_tnt = Button(fenster, text = "TNT Serie HD", command = startstream)

This works as I want.

The following one doesn't work like I want it and I have no idea why not.

def startstream(url):
    args = ['C:/Program Files/VideoLAN/VLC/vlc.exe', url]
    subprocess.call(args)

# Buttons
button_tnt = Button(fenster, text = "TNT Serie HD", command = startstream('http://dreambox:8001/1:0:19:7B:B:85:C00000:0:0:0:'))

With the first code, the window appears and nothing happen. If I click the button, the stream starts, perfect.

Second code: I run the script and the stream starts immediatelly. After I close the VLC I cannot reopen the stream over the button, it has no function.

But I want to use the second code. I have more than one button and so I can only change the argument for each button. With the first code I have to write a new function for every stream.

Please help me :(

Thanks!

3
  • Which GUI kit are you working with? I can spot the issue - command=startstream(blah) is calling startstream immediately. You need to still have command=startstream but a way of passing the arg to it Commented May 16, 2014 at 21:48
  • Sorry, forgot to tell you. tkinter Commented May 16, 2014 at 21:49
  • unrelated: use subprocess.Popen instead of subprocess.call to avoid blocking the GUI Commented May 17, 2014 at 13:10

3 Answers 3

2

You're executing startstream instead of assigning it*. To give it arguments, use the following:

button_tnt = Button(fenster, 
                    text="TNT Serie HD",
                    command= lambda: startstream('http://dreambox:8001/1:0:19:7B:B:85:C00000:0:0:0:'))

*specifically, you're executing startstream(..) and assigning the result of that to command.
The lambda will instead create the function that command will call when clicked.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow. Works amazing. But I have no idea why :D I think I have to do some research here. Thank you very much!
No problem - if you want further reading into why this works, read up on lambda (anonymous) functions. Essentially, instead of assigning the return value of startstream(..) to command (Which we don't want of course), lambda returns the function itself.
2

The reason is, that when constructing Button, purpose of command argument is to define a function to be called, when the button is called.

Your first example does that, it assigns name of a function to call.

In second you do not assign a function, but result of calling the startstream call. That is why it starts streaming immediately.

If you really want to assign a function using the url, you may do so this way:

from functools import partial

# Buttons
button_tnt = Button(fenster, text = "TNT Serie HD", command = partial(startstream, 'http://dreambox:8001/1:0:19:7B:B:85:C00000:0:0:0:'))

The partial will create a new function, which will call startstream with given parameter.

Comments

1

The trouble is that in your second example you're not passing arguments to startstream on click, you're calling startstream when you create your button.

button_tnt = Button(fenster, 
                    text = "TNT Serie HD", 
                    command = startstream('http://dreambox:8001/1:0:19:7B:B:85:C00000:0:0:0:'))
                                       # ^ Invocation takes place right here.

1 Comment

Interesting information. Thanks so far. Do you have an idea how to solve?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.