j2f
Posts: 4
Joined: Mon Sep 23, 2019 4:17 pm

tkinter not working - permissions issue?

Sun Sep 29, 2019 2:00 pm

Hi everyone, I am stuck with a python tkinter problem and have been unable to find a solution despite days of trawling through forums. Can anyone help? The set-up is relatively simple: I have an html page calling a php script which initiates a python program. All good so far. The python program uses tkinter, and that's where it gets confusing: the program (with tkinter code) works fine as a standalone program, but when called by the php script, tkinter doesn't work. I suspect it's a permission issue.

The three programs are in /var/www/html/ (with www-data:www-data ownership) and are as follows:

HTML front-end:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>HTML Front-End</title>
</head>
<body>
	<br>
	<form action="call.php">
  	<input type="submit" value="click">
	</form>
</body>
</html>
PHP script:

Code: Select all

<?php 
$python_program = system("python3 test.py", $retval);      	
echo '
</pre>
<hr />Return value: ' . $retval . '   ';
?>
Python program:

Code: Select all

#!/usr/bin/python3.7
from tkinter import Tk 
window = Tk()	
window.geometry("400x400")
window.title(‘Test Window’)
window.mainloop()
By the way, the return value from the php script is 1 (which I guess is not really useful). Would appreciate some help. Thanks.

scotty101
Posts: 3735
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter not working - permissions issue?

Mon Sep 30, 2019 12:14 pm

What does the PHP code return?

Its probably something to do with not knowing where the correct display device is.

This isn't a great way to trying to get a tkinter window to be displayed. Perhaps you can explain, at a high level, what you are trying to achieve as there is likely to be a better to to achieve it than calling a python script from PHP.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: tkinter not working - permissions issue?

Mon Sep 30, 2019 12:22 pm

What are you trying to achieve?

Webservers and CGI programs (in disguise as PHP) are poles apart from GUI programs like Tk/TCL, TkInter and such-like. I can't imagine why you think you'd be able to run a GUI in a CGI environment.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

j2f
Posts: 4
Joined: Mon Sep 23, 2019 4:17 pm

Re: tkinter not working - permissions issue?

Mon Sep 30, 2019 3:54 pm

Thanks for the feedback. Eventually, the python program will be called from a WordPress page. For the time being, I am testing the call from a standalone webpage: html > php > python program. Note that the python program works fine if called directly (tkinter opens the window in this case). It's only when called from the php script that the window is not opening. Any idea what the cause might be?

User avatar
DougieLawson
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: tkinter not working - permissions issue?

Mon Sep 30, 2019 4:17 pm

j2f wrote:
Mon Sep 30, 2019 3:54 pm
Any idea what the cause might be?
Because you can't run TkInter programs in a CGI enviroment, it doesn't have (and can't have) a DISPLAY.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

j2f
Posts: 4
Joined: Mon Sep 23, 2019 4:17 pm

Re: tkinter not working - permissions issue?

Mon Sep 30, 2019 5:06 pm

Thanks! Will have to do it the hard way ;)

Return to “General programming discussion”