我不是程序员,我是一名艺术家,正在学习Python以制作艺术装置。到目前为止,我是在Youtube和Stackoverflow上自学。
话虽如此...
我从API中获取文本,然后将其显示在标签中。原始文本看起来像这样:
{"word":"hello","synonyms":["hi","how-do-you-do","howdy","hullo"]}
,单词根据用户输入而变化。
我只想显示括号内的单词,不要包含标点符号。经过了很多Stackoverflow,我发现re.sub(r"\W", "", text, flags=re.I)
(天哪,我不知道它的意思,只知道w、W表示字母/非字母)。这给了我:
"wordhellosynonymshihowdoyoudohowdyhullo
叹气。我怎样才能保留空格?我认为我可以自己去掉前面的17个单词。
完整代码(我很尴尬)。我知道其中部分已经损坏了,我正在逐个处理问题。最终项目是一个程序,返回用户输入的单词的同义词,然后询问用户输出是否正确。结果最终会显示在图表中。有时候,输出结果是有意地错误的,我不需要这个被修复。
'''
import requests
from tkinter import *
import tkinter.messagebox
from tkinter import font
import re
import string
root = Tk ()
Graph = Tk ()
def create_grid(event=None):
w = c.winfo_width() # Get current width of canvas
h = c.winfo_height() # Get current height of canvas
c.delete('grid_line') # Will only remove the grid_line
# Creates all vertical lines at intevals of 100
for i in range(0, w, 100):
c.create_line([(i, 0), (i, h)], tag='grid_line')
# Creates all horizontal lines at intevals of 100
for i in range(0, h, 100):
c.create_line([(0, i), (w, i)], tag='grid_line')
c = Canvas(Graph, height=1600, width=1600, bg='white')
c.pack(fill=BOTH, expand=True)
c.bind('<Configure>', create_grid)
#lets you press enter for query instead of clicking w mouse
#the "forget" objects deletes the button and the widget.
# Should figure out either a timeout, restart, or back-button.
def onReturn(*args):
command = Buttonclick()
Button.pack_forget(Button_1)
Entry_1.pack_forget()
tkinter.messagebox.showinfo("Help!","This installation measures the accuracy of synoymns delivered. In the next window, please select 'yes' if your synonym was correct, and 'no' if it was not.")
answer = tkinter.messagebox.askquestion("Accurate?", "Synonym Accurate?")
## Immeidately above and below is the messagebox text. Will eventually connect to a grid.
if answer == "yes":
print("thanks!")
if answer == "no":
print("FUCK. Sorry.")
### Add yes/no actions for message box!
#Gets the user entry when button is pressed, either by mouse or enter key.
def Buttonclick():
Entry_1.get()
# this is the API that gets the synonyms - the url must be www.URLURLURL.URL.com/words/ENTRY HERE!!/typeof
#DO NOT FUCKING TOUCH THIS. IT'S MAGIC AND I DO NOT CONTROL IT.
url = "https://wordsapiv1.p.rapidapi.com/words/"+Entry_1.get()+"/synonyms"
headers = {
'x-rapidapi-host': "wordsapiv1.p.rapidapi.com",
'x-rapidapi-key': "myapikey"
}
punct = "!#$%&'()*+,-./:;?@[\]^_`{|}~"
response = requests.request("GET", url, headers=headers,)
newstring = response
text = response.text
print(response.text)
label = Label(root, text= re.sub(r"\W", "", text, flags=re.I), font="helv 18", bg="black", fg="white", )
label.pack()
testing = Button(root, text="Press Spacebar to Restart", font="Helv 24", bg="red", fg="white",command=Spacebar)
testing.bind("<space>",Spacebar)
testing.pack()
def Spacebar():
root.configure(background='blue')
root.geometry("1600x1600+50+50")
Entry_1 = Entry(root, bg="black", fg="White", font="Helv 48")
Entry_1.bind("<space>", onReturn)
Entry_1.pack(expand=1)
Button_1 = Button(root, text="Type Word, Press Enter For Synonym", font="Helv 24", bg="blue", fg="white", command=Buttonclick)
Button_1.pack(fill=X, expand=1)
Label.forget()
#Initial button and text entry box, sized correctly.
root.configure(background='blue')
root.geometry("1600x1600+50+50")
Entry_1 = Entry(root, bg="black", fg="White", font="Helv 48")
Entry_1.bind("<Return>", onReturn)
Entry_1.pack(expand=1)
Button_1 = Button(root, text="Type Word, Press Enter For Synonym", font="Helv 24", bg="blue", fg ="white", command=Buttonclick)
Button_1.pack(fill=X, expand=1)
root.mainloop() ```