简体   繁体   English

python,tkinter,将CSV文件保存在远程服务器上-没有$ DISPLAY环境变量

[英]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. 我正在编写一个python脚本,以提取MySQL db中项目的匹配数据,然后将该数据写入csv文件。 I'd like the program to prompt the user to save this csv file on their computer (I'm using Tkinter for this). 我希望该程序提示用户将该csv文件保存在他们的计算机上(为此,我正在使用Tkinter)。 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: 该脚本在我的本地计算机上都可以正常运行,但是当我尝试使用CGI来实现它,或者甚至将其放置在安装了所有软件包(非CGI)的远程服务器上时,出现以下错误:

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 我搜索并看到了一些其他文章,其中提到了类似的错误,但是涉及到matplotlib(我没有使用): 当未定义DISPLAY时,使用matplotlib生成PNG

Or possibly making adjustments to the $DISPLAY/ENV variables or variations in X (all of which I'm pretty unfamiliar with). 或者可能对$ DISPLAY / ENV变量或X中的变量进行调整(所有这些我都不熟悉)。

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?). 最终,我希望其他用户可以访问该程序,所以我想知道我需要对脚本进行哪些调整(除了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. 首先:不能将Tkinter与CGI结合使用-两种不同的技术。

Second: running Tkinker on server will require X server on user computer, and maybe he will need to use "ssh port tunneling" technology. 第二:在服务器上运行Tkinker将需要在用户计算机上使用X server ,也许他将需要使用"ssh port tunneling"技术。 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. 当您尝试在远程计算机上运行GUI程序时,该程序将尝试显示在连接到服务器的显示器上-而不是在屏幕上。 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. 如果您需要在远程计算机上运行的程序,则需要client-server技术。 It needs client - program with Tkinter (or other GUI) on local computer, and server - program without GUI on remote computer. 它需要client (在本地计算机上具有Tkinter(或其他GUI)的程序)和server (在远程计算机上没有GUI的程序)。 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 ). 如我所见,您可以在本地计算机上使用您的程序( client )并与远程数据库( server )连接。 You could give your program ( client ) to other users and they would have access to remote database ( server ). 您可以将程序( client )提供给其他用户,其他用户可以访问远程数据库( 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). 如果您不想将client给其他人(因为您必须在他们的计算机上安装Python和模块),则需要web page (使用CGI或WSGI和某些Web框架)。

Web server on remote server works as server and user web browser works a client . 远程服务器上的Web服务器充当server而用户Web浏览器充当client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tkinter.TclError:没有显示名称,没有$ DISPLAY环境变量python - tkinter.TclError: no display name and no $DISPLAY environment variable python macOS、Tkinter、Python (Macports) 给出错误“没有 $DISPLAY 环境变量” - macOS, Tkinter, Python (Macports) gives error "no $DISPLAY environment variable" tkinter,python和seaborn的问题:_tkinter.TclError:没有显示名称,没有$ DISPLAY环境变量 - Issue with tkinter, python and seaborn: _tkinter.TclError: no display name and no $DISPLAY environment variable 将python变量保存到csv - saving python variable to csv TkInter 错误:没有显示名称和 $DISPLAY 环境变量 - TkInter error: no display name and no $DISPLAY environment variable 在 Python 中实例化画布:tkinter.TclError:无显示名称且无 $DISPLAY 环境变量 - Instantiating a Canvas in Python : tkinter.TclError: no display name and no $DISPLAY environment variable 使用python动态保存csv文件 - Saving csv files dynamically with python Python tkinter 条目 Function 不将输入保存到变量中 - Python tkinter Entry Function not saving input into variable DietPI:_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量 - DietPI: _tkinter.TclError: no display name and no $DISPLAY environment variable Ubuntu:_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量 - Ubuntu: _tkinter.TclError: no display name and no $DISPLAY environment variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM