Python GUI programming using drag and drop, also including stdout redirection

I am new to programming and new to python. I just developed my first script, it will exist a file, but at the moment only from the command line.

This is just a hobby for me, so my work does not depend on it :-)

I spent a few days trying to figure out the development of python gui and came to the conclusion that I should be stupid.

I looked at wxpython and Tkinter and don't understand a single one, although Tkinter seems to be the easier of the two. I even looked at wysiwyg tools like Boa Contrictor and wxglade. I don’t even understand how to use them. I would rather just use my editor and code anyway.

My problem is this:

I would like to create a desktop window with 1 or two objects, depending on what works best. If there is only one object, then a text field of some type, if 2 objects, then a text field and an image.

I want to be able to drag and drop a file from the file manager and drop it in the script window, this is just passing the file names to my script.

I, than I want to redirect stdout to an object in my working window, so that all script output is displayed in the desktop window.

I'm not sure if one object can do something or not. If this is possible than just a text field, just drop the files onto the image and redirect the output to the text field.

I found drag and drop examples on the Internet, but nothing that included stdout redirection, and I could not successfully modify any of the examples I came across.

If any sole has time to demonstrate how to achieve what I want and explain how I would appreciate its work with dignity!

---- EDIT ----

I play with two examples and managed to put hash 2 together to get what I wanted to work. The code is below. It is not cleared yet (old comments, etc.), but it works.

#!/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() 

Examples: python drag explorer files into tkinter input widget

As well as an example provided courtesy of Noelkd.

For this code to work, you must create a wrapper from the first example. Also, at present, the code simply displays the dragged file in the window, however, the in-place variable must be passed to the script that passes the gui interface.

+4
source share
2 answers

If you want to use 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() 

What this does is create a window with a button and a text box with stdout redirection.

Currently, in Tkinter, you cannot drag and drop files into the open tk window (it is possible if you use tkdnd ), so I included another way to get the file path.

The way I turned on to select the file is the askopenfilename dialog from tkFileDialog, this opens the file browser and the selected path file is returned as a string.

If you have any questions or this is not quite what you are looking for, leave a comment!

+4
source

Look at the GTK. This is a really powerful library. Not the easiest, it is a fact, but as soon as you understand how everything works, it becomes much easier. Here is the official textbook

If oyu still uses Python2, I think you should use PyGTK , but it has been replaced by gl (which is described in the tutorial above). A good tutorial for PyGTK can be found here .

For a static interface, you can use glade , which creates XML files that GTKBuilder then reads to create a β€œreal” interface. The first tutorial I found is available here

0
source

Source: https://habr.com/ru/post/1491850/


All Articles