简体   繁体   中英

python, tkinter, saving csv files on remote server - no $DISPLAY environment variable

I'm writing a python script to pull out hit data for items in a MySQL db and then write that data to a csv file. I'd like the program to prompt the user to save this csv file on their computer (I'm using Tkinter for this). The script all works fine on my local machine but when I try to implement it using CGI or even just placing it on a remote server with all of the packages installed (non-CGI), I get the following error:

Traceback (most recent call last):
  File "ct_hits.py", line 29, in <module>
    root = Tkinter.Tk()
  File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 1643, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

The essence of the script is as follows:

#!/usr/bin/python
import MySQLdb
import csv
import getpass
import Tkinter, tkFileDialog, Tkconstants

uname = raw_input("Database user id: ")
pword = getpass.getpass("Database password: ")

try:
    db = MySQLdb.connect("mysqldbservername",uname,pword,"databasename")
except:
    print "Invalid username and/or password!"
    quit()

formats = [('Comma Separated values', '*.csv'), ]
root = Tkinter.Tk()
file_name = tkFileDialog.asksaveasfilename(parent=root, filetypes=formats, title="Save as...",defaultextension='.csv')
writer = csv.writer(open(file_name, 'w'))
writer.writerow(["Hits for date range:", str(date_start) + " to " + str(date_end)])
writer.writerow(["ID:", "Title:", "Hits:"])

I've searched and seen a few other posts that mention similar errors but these involve matplotlib (which I'm not using): Generating a PNG with matplotlib when DISPLAY is undefined

Or possibly making adjustments to the $DISPLAY/ENV variables or variations in X (all of which I'm pretty unfamiliar with).

Eventually I'd like the program to be accessible to other users so I'm wondering what adjustments I need to make to the script (are there other options besides Tkinter?). Or do I need to make adjustments to environment or path variables on the server? Thanks.

First: you can't use Tkinter with CGI - two different technologies.

Second: running Tkinker on server will require X server on user computer, and maybe he will need to use "ssh port tunneling" technology. When you try to run GUI program on remote computer then this program try to display on monitor direclty connected to server - not on your screen. There is a lot work to send this to your screen.

If you need program running on remote computer than you need client-server technology. It needs client - program with Tkinter (or other GUI) on local computer, and server - program without GUI on remote computer. They are connected by sockets (network connection).

As I see You could use you program ( client ) on local computer and connect with remote database (it your server ). You could give your program ( client ) to other users and they would have access to remote database ( server ).

If you don't want to give client to others - because you will have to install Python and modules on their computer - then you need web page (using CGI or WSGI and some web framework).

Web server on remote server works as server and user web browser works a client .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM