Home > Mobile >  How do you launch a Tkinter application from a django website
How do you launch a Tkinter application from a django website

Time:08-11

I created a Django website and I want to launch a tkinter program if the user clicks on a button. Can anyone tell me how to do that?

Any help is appreciated.

CodePudding user response:

Generally, websites cannot cause other programs on the user's computer to run just by clicking a button on a webpage. The next-best thing is a specialized link that the client understands is associated with another installed application. See: How can I launch my windows app when a users opens a url?

If you happen to be running the django server on the same local system AND is running in the foreground by the same currently logged in user, you could also invoke a GUI program this way, as well for example using subprocess or os.system. Though, this would be quite an odd way to do this.

def my_view():  # Technically works, but in very limited practical use cases.
    subprocess.Popen(['python', 'my-tk-app.py'])

Because tkinter also doesn't like to be run in any other thread besides the main thread, and other event loop-related issues, it's not really practical to invoke a tkinter app directly from a Django view, which usually will be running in a separate thread and needs to not be blocked.

For example, this would not work:

def my_view():
    root.mainloop() # start the TK app
    # fails because tk only wants to start in the main thread
    # even if TK _could_ start, the view would eventually timeout

If you need a local tkinter app to also interface with a django server, you're almost always going to be better off by making your GUI app a client of the server -- that is to say, your tkinter app should use HTTP(S) or similar mechanism to communicate with Django.

  • Related