私はそれほど難しくないと思っていることをやっているうちに何か問題があります。私は、与えられたDOI(研究論文のためのユニークな識別)を使用し、それをウェブサイトに送信し、Bibtex形式のテキスト結果をユーザに返して結果を選択し、コピーして貼り付けることができるプログラムを作成したい。Python Tkinterプログラムが与えられたURLからテキストを返す
下記の(Python 3.x)コードは私の試みです(TkinterとPythonのプログラミングで初めて)。私はそれが期待できるようにラベルを更新できるようには見えない。私は本当に「自己」の使用を理解していないことに気がつくかもしれませんが、それは別の時間のための別の質問です。あなたが少しジャズアップしたい場合は、自由に感じてください!
ありがとうございました。
import urllib.request
from tkinter import *
##This is the logic of what I want to do
# x = urllib.request.urlopen('http://api.crossref.org/works/10.1098/rsta.2010.0348/transform/application/x-bibtex')
# data = x.read()
# print(data)
'''Small attempt at a program to take a DOI input (unique address for research papers) and
return the Bibtex formatted result from the above website in a copy/pastable form.
It should handle HTTP errors and be reusable. While the program below is my attempt,
any solution that does the above would be appreciated.'''
class MyFirstGUI:
def __init__(self, master):
self.master = master
master.title("DOI to Bibtex Tool")
self.label1 = Label(master, text="Enter DOI")
##Give a default, customisable DOI value
self.v = StringVar(root, value='10.1098/rsta.2010.0348')
self.entry1 = Entry(master, bd =5, textvariable=self.v)
self.label1.pack()
self.entry1.pack()
self.submit = Button(master, text ="Submit", command = self.update)
self.submit.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
##Here I want to produce the result of my http request call
self.v = StringVar()
self.v.set("Output area for result of HTTP request to be updated when you press submit\n"
+"(ideally highlightable/copy pastable)")
self.output_label = Label(master, text=self.v.get())
self.output_label.pack()
##This here is an experimental method to get highlightable text, which is desirable but can't get this to work either.
#Uncomment for example
# self.w = Text(master, height=100)
# self.w.insert(1.0, self.v.get())
# self.w.pack()
# self.w.configure(bg=self.master.cget('bg'), relief=FLAT)
# self.w.configure(state="disabled")
def update(self):
doi = str(self.entry1.get()) ##Get the user inputted DOI
print(str(self.entry1.get()))
url = 'http://api.crossref.org/works/'+ doi + '/transform/application/x-bibtex'
print(url)
try:
x = urllib.request.urlopen(url)
except urllib.error.URLError as e:
print(str(e))
##Show user an error if they put in the wrong DOI
self.v.set(str(e)) ##This doesn't update. Something like label.update() would be nice
else:
##Update the output area to the returned form of the text entry, ideally highlightable for copying
data = x.read()
self.v.set(data) ##This doesn't update. Something like label.update() would be nice
print(data) ##I also want it to interpret the escape characters, so \n becomes linebreak etc
root = Tk()
root.geometry("600x400")
my_gui = MyFirstGUI(root)
root.mainloop()
FWIWでは、「textvariable」を付けずにラベル内のテキストを更新することもできます。 –
@Bryan Oakleyはい、そうですが、.config()でテキストを設定することもできます: 'self.output_label.config(text = data)' –
これは私が必要なものです。結果をクリップボードに選択/コピーする方法に関するアイデアはありますか? –