我写了一个脚本,将所有图像从给定的页面URL上传到我的imgur帐户并打印输出。它工作正常,但现在我的一个非技术性朋友也想使用该应用程序,但他不想使用命令行,所以我试图制作一个GUI。
我是GUI制作的新手。我做了一些应该从GUI获取输入,将输入传递给函数并输出上传的URL(我的imgur帐户中的URL)。
我的程序调用函数正常。网址被转换,但窗口停止响应,但程序正在运行。
但在第一个功能完成后,窗口再次开始响应
然后我可以打印转换后的网址
以下是代码:
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()