我是编程新手,也是python新手。我刚刚开发了我的第一个脚本,它处理文件,但目前只从命令行。
这只是我的一个爱好,所以我的工作并不依赖于它:-)
我现在已经花了几天的时间来尝试python gui开发&我得出的结论是我一定很愚蠢。
我看过wxpython和Tkinter &我也不理解,尽管Tkinter似乎是这两者中比较容易的一个。我甚至看过像Boa Contrictor和wxglade这样的wysiwyg工具。我甚至不知道如何使用它们。无论如何,我更喜欢使用我的编辑器&手动编写代码。
我的问题是:
我想创建一个包含1个或2个对象的桌面窗口,具体取决于哪个对象效果最好。如果只有一个对象,则是某种类型的文本框;如果是两个对象,则是文本框&图像。
我希望能够从文件管理器中拖动文件并将它们放到我的脚本窗口中,这只是为了将文件名传递给我的脚本。
然后,我想将stdout重定向到我的桌面窗口中的一个对象,以便所有脚本输出都显示在桌面窗口中。
我不确定一个对象是否可以同时做这两件事。如果可以,那么只需一个文本框就足够了,否则将文件拖放到图像上,并将输出重定向到文本框。
我在web上找到了拖放的例子,但是没有包含stdout重定向的例子,我还没能成功地修改我遇到的任何例子。
如果某个好心的sole有时间演示如何实现我想要的东西,并解释它是如何工作的,我将非常感激!
--编辑
我一直在尝试两个示例,并设法将这两个示例散列在一起,以便得到我想要的结果。代码如下。它还没有被清理(旧的评论等等...),但它是有效的。
#!/usr/bin/python
# The next two lines are not necessary if you installed TkDnd
# in a proper place.
import os
from Tkinter import *
os.environ['TKDND_LIBRARY'] = '/home/clinton/Python/tkdnd2.6/'
import Tkinter
from untested_tkdnd_wrapper import TkDND
class Redir(object):
# This is what we're using for the redirect, it needs a text box
def __init__(self, textbox):
self.textbox = textbox
self.textbox.config(state=NORMAL)
self.fileno = sys.stdout.fileno
def write(self, message):
# When you set this up as redirect it needs a write method as the
# stdin/out will be looking to write to somewhere!
self.textbox.insert(END, str(message))
root = Tkinter.Tk()
dnd = TkDND(root)
textbox = Tkinter.Text()
textbox.pack()
def handle(event):
event.widget.insert(END, event.data)
content = textbox.get("0.0",Tkinter.END)
filename = content.split()
dnd.bindtarget(textbox, handle, 'text/uri-list')
#Set up the redirect
stdre = Redir(textbox)
# Redirect stdout, stdout is where the standard messages are ouput
sys.stdout = stdre
# Redirect stderr, stderr is where the errors are printed too!
sys.stderr = stdre
# Print hello so we can see the redirect is working!
print "hello"
# Start the application mainloop
root.mainloop()
例如:python drag and drop explorer files to tkinter entry widget
还有Noelkd亲切提供的例子。
为了让这段代码正常工作,您必须从第一个示例创建包装器。此外,目前的代码只在窗口中显示拖动的文件,但是有一个变量要传递给在gui界面后面运行的脚本。
发布于 2013-07-17 20:33:58
如果您想使用Tkinter:
from Tkinter import *
import tkFileDialog
class Redir(object):
# This is what we're using for the redirect, it needs a text box
def __init__(self, textbox):
self.textbox = textbox
self.textbox.config(state=NORMAL)
self.fileno = sys.stdout.fileno
def write(self, message):
# When you set this up as redirect it needs a write method as the
# stdin/out will be looking to write to somewhere!
self.textbox.insert(END, str(message))
def askopenfilename():
""" Prints the selected files name """
# get filename, this is the bit that opens up the dialog box this will
# return a string of the file name you have clicked on.
filename = tkFileDialog.askopenfilename()
if filename:
# Will print the file name to the text box
print filename
if __name__ == '__main__':
# Make the root window
root = Tk()
# Make a button to get the file name
# The method the button executes is the askopenfilename from above
# You don't use askopenfilename() because you only want to bind the button
# to the function, then the button calls the function.
button = Button(root, text='GetFileName', command=askopenfilename)
# this puts the button at the top in the middle
button.grid(row=1, column=1)
# Make a scroll bar so we can follow the text if it goes off a single box
scrollbar = Scrollbar(root, orient=VERTICAL)
# This puts the scrollbar on the right handside
scrollbar.grid(row=2, column=3, sticky=N+S+E)
# Make a text box to hold the text
textbox = Text(root,font=("Helvetica",8),state=DISABLED, yscrollcommand=scrollbar.set, wrap=WORD)
# This puts the text box on the left hand side
textbox.grid(row=2, column=0, columnspan=3, sticky=N+S+W+E)
# Configure the scroll bar to stroll with the text box!
scrollbar.config(command=textbox.yview)
#Set up the redirect
stdre = Redir(textbox)
# Redirect stdout, stdout is where the standard messages are ouput
sys.stdout = stdre
# Redirect stderr, stderr is where the errors are printed too!
sys.stderr = stdre
# Print hello so we can see the redirect is working!
print "hello"
# Start the application mainloop
root.mainloop()
这样做的目的是创建一个带有按钮和文本框的窗口,并使用stdout重定向。
目前在Tkinter中,你不能将文件拖放到打开的tk窗口上(如果你使用tkdnd,就可以),所以我提供了一种获取文件路径的不同方法。
我所包含的选择文件的方式是来自tkFileDialog的askopenfilename对话框,这将打开一个文件浏览器,所选的路径文件将以字符串的形式返回。
如果你有任何问题,或者这不能完全满足你的需求,请留下评论!
发布于 2013-07-17 19:51:37
看看GTK。这是一个非常强大的库。这不是最简单的,这是一个事实,但是一旦你理解了事情是如何工作的,它就变得容易得多。这是official tutorial
如果oyu还在使用Python2,我认为你应该使用PyGTK,但它已经被gl (在上面的教程中描述)所取代。在here上可以找到一个很好的PyGTK教程。
对于静态接口,您可以使用glade来生成XML文件,然后由GTKBuilder读取这些文件来创建“真正的”接口。我找到的第一个教程是here
https://stackoverflow.com/questions/17698138
复制相似问题