2017-01-11 10 views
0

私はそれほど難しくないと思っていることをやっているうちに何か問題があります。私は、与えられた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() 

出力例: example output

答えて

1

あなたが望むことを達成するために実際にStringVarを使用する必要はありません。 以下のコードは、テキストウィジェットとStringVarを使用せずにコピー/貼り付け可能なテキストを取得する方法を示しています。

更新するたびに、テキストウィジェットの状態を通常に戻して、ウィジェットコンテンツを更新するためのupdate_textメソッドを書きました。

import urllib.request 
from tkinter import * 


class MyFirstGUI(Tk): 

    def __init__(self): 
     # create main window by calling the __init__ method of parent class Tk 
     Tk.__init__(self) 
     self.geometry("600x400") 
     self.title("DOI to Bibtex Tool") 

     label1 = Label(self, text="Enter DOI") 
     label1.pack() 

     ##Give a default, customisable DOI value 
     self.entry1 = Entry(self, bd=5) 
     self.entry1.insert(0, '10.1098/rsta.2010.0348') 
     self.entry1.pack() 

     submit = Button(self, text ="Submit", command = self.update) 
     submit.pack() 

     close_button = Button(self, text="Close", command=self.quit) 
     close_button.pack() 

     ##Here I want to produce the result of my http request call 
     self.w = Text(self, relief='flat', 
         bg = self.cget('bg'), 
         highlightthickness=0, height=100) 
     # trick to make disabled text copy/pastable 
     self.w.bind("<1>", lambda event: self.w.focus_set()) 
     self.w.insert('1.0', "Output area for result of HTTP request to be updated when you press submit\n" 
             +"(ideally highlightable/copy pastable)") 
     self.w.configure(state="disabled", inactiveselectbackground=self.w.cget("selectbackground")) 
     self.w.pack() 

     self.mainloop() 

    def update_text(self, new_text): 
     """ update the content of the text widget """ 
     self.w.configure(state='normal') 
     self.w.delete('1.0', 'end') # clear text 
     self.w.insert('1.0', new_text) # display new text 
     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: 
      ##Show user an error if they put in the wrong DOI 
      self.update_text(str(e)) 

     else: 
      ##Update the output area to the returned form of the text entry, ideally highlightable for copying 
      data = x.read() 
      self.update_text(data) 

if __name__ == '__main__': 
    my_gui = MyFirstGUI() 
1

は、これが私の作品:

self.output_label = Label(master, textvariable=self.v) 

textvariableは、ラベル内のテキストを更新することができます。

+0

FWIWでは、「textvariable」を付けずにラベル内のテキストを更新することもできます。 –

+0

@Bryan Oakleyはい、そうですが、.config()でテキストを設定することもできます: 'self.output_label.config(text = data)' –

+0

これは私が必要なものです。結果をクリップボードに選択/コピーする方法に関するアイデアはありますか? –

関連する問題