使用拖放进行Python GUI编程,还包含stdout重定向

问题描述 投票:5回答:2

我是编程的新手,也是python的新手。我刚开发了我的第一个脚本,它是prosesses文件,但目前只是从命令行开始。

这对我来说只是一个爱好,所以我的工作不依赖于它:-)

我花了几天时间试图了解python gui开发并得出结论我一定是傻瓜。

我看过wxpython和Tkinter并且也不明白,虽然Tkinter看起来更容易。我甚至看过像Boa Contrictor和wxglade这样的所见即所得的工具。我甚至不明白如何使用它们。无论如何,我更愿意手动使用我的编辑器和代码。

我的问题是:

我想创建一个带有1个或2个对象的桌面窗口,具体取决于最佳效果。如果只有一个对象,则为某种文本框,如果是2个对象,则为文本框和图像。

我希望能够从文件管理器中拖动文件并将其放在我的脚本窗口中,这只是将文件名传递给我的脚本。

我想将stdout重定向到桌面窗口中的对象,以便所有脚本输出都出现在桌面窗口中。

我不确定一个物体是否能做到这两件事。如果它可以只是一个文本框就足够了,否则将文件拖放到图像上并将重定向的输出转到文本框。

我在网上找到了拖放示例但没有包含stdout重定向的内容,而且我无法成功修改我遇到过的任何示例。

如果某种鞋底有时间展示如何实现我想要的东西并解释它的工作方式,我将非常感激!

----编辑----

我一直在玩2个例子,并设法将2个哈希,以便得到我想要的工作。代码如下。它尚未清理(旧评论等......),但它确实有效。

#!/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界面后面运行的脚本。

python user-interface tkinter wxpython stdout
2个回答
5
投票

如果你想使用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对话框,这会打开一个文件浏览器,所选的路径文件将作为字符串返回。

如果您有任何问题,或者这不是您想要的,请发表评论!


1
投票

看看GTK吧。它是一个非常强大的库。这不是最简单的,这是一个事实,但是一旦你理解了事情是如何运作的,它就变得容易了。这是official tutorial

如果oyu仍在使用Python2,我认为你应该使用PyGTK但它已被gl替换(在上面的教程中有描述)。可以找到一个关于PyGTK的好教程here

对于静态接口,您可以使用glade生成XML文件,然后由GTKBuilder读取以创建“真实”接口。我发现的第一个教程是here

© www.soinside.com 2019 - 2024. All rights reserved.