0

I wrote a script that uploads all images from an given page URL to my imgur account and prints the output. It works fine, but now one of my non-technical friends wants too use the app but he does not want to use the command line, so I tried to make an GUI.

I am a newbie to GUI making. I made something that should take an input from the GUI, pass the input to a function and output the urls uploaded (urls in my imgur account).

My program calls the function fine. urls get converted but the window stops responding but the program is running.

but after the first function completes the window starts responding again

then i can print the converted urls

Here is the code:

from bs4 import BeautifulSoup
from Tkinter import *
import Tkinter as tk
import requests, pyimgur, time
from urlparse import urljoin

urllist = []

def callback():
    global urllist
    for i in urllist:
        tex.insert(tk.END, i + '\n')
        tex.see(tk.END)

def connection(url):
    #global urllist
    print '\n\n', url, '\n\n'
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    urllist = parse(soup, url)


def parse(parsed, url):
    global urllist

    client_id = 'myid'

    image_links = [i.get('src') for i in parsed.find_all("img")]

    for u in image_links:
        if u.split('.')[-1] == 'jpg':

            try:
                time.sleep(1)
                im = pyimgur.Imgur(client_id)
                uploaded_image = im.upload_image(url=urljoin(url, '%20'.join(u.split(' '))), title="shady")

                #printing the uploaded image links
                print uploaded_image.link

                #calling the function to output in the gui
                urllist.append(uploaded_image.link)

            except Exception as e:
                print e
    #return urllist


#url = raw_input("Enter the url")
#url = 'https://www.oceanreefresorts.com/beaches-south-walton-rentals/adagio'
#connection(url)

root = Tk()
tex = tk.Text(master=root)
tex.pack(side=tk.RIGHT)
root.config(height = 360, width = 640)
root.title('Url Conversion App')
root.resizable(False, False)

firstName = StringVar()
userAge = IntVar()

tk.Label(root, text = 'URL: ').place(x=20, y=300)
tk.Entry(root, width = 30, textvariable = firstName).place(x=115, y=300)

convertimages = tk.Button(root, text = 'CONVERT', command= lambda: connection(firstName.get())).place(x=20, y=350)
buttonTestPrint = tk.Button(root, text = 'print', command=callback).place(x=20, y=320)

root.update()

root.mainloop()
Shantanu Bedajna
  • 559
  • 10
  • 34
  • 1
    what is the purpose of `except Exception as e: pass` ? It is just suppressing a `NameError` being raised by `callback` amongst other things, if you aren't going to do any error handling try to only catch error types that you want to suppress. – Tadhg McDonald-Jensen Jun 02 '17 at 16:34
  • first i thought i would print the error then i just passed it sorry i will edit it now – Shantanu Bedajna Jun 02 '17 at 16:45
  • 1
    My point was that it was the source of half your issue, now that you've edited it you probably just want to add `root.update()` somewhere it'd get called occasionally in your large functions so it gives the GUI a chance to update itself occasionally. – Tadhg McDonald-Jensen Jun 02 '17 at 17:02
  • after editing it works and outputs in the gui too .. any way to stop the not responding message ? – Shantanu Bedajna Jun 02 '17 at 17:12
  • 1
    your computer will have a time interval for "if a program doesn't respond within ___ milliseconds show a not responding window" so if you call `root.update()` or allow it back into the `mainloop` (by not running any functions) in a lower time interval then that it won't give you the window. There are some possible solutions [in this thread](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) but I think calling `root.update()` would be the simplest one. – Tadhg McDonald-Jensen Jun 02 '17 at 17:25
  • You want to add a thread. The GUI is hanging because it's waiting for things to complete. Here's an example someone posted: https://stackoverflow.com/questions/16745507/tkinter-how-to-use-threads-to-preventing-main-event-loop-from-freezing – Gardener85 Jun 02 '17 at 18:24

0 Answers0