|
| 1 | +from tkinter import * |
| 2 | +from gnewsclient import gnewsclient |
| 3 | +from gnewsclient import utils |
| 4 | +from tkinter import ttk |
| 5 | + |
| 6 | + |
| 7 | +# function to set attributes(language, location, topic), and then fetch news accordingly |
| 8 | +# we have variables topic_query, language_query, location_query for storing attributes |
| 9 | +def news(): |
| 10 | + global location_query, language_query,topic_query,edition_query,client,status |
| 11 | + client.query=None # because search query overrides all other parameters |
| 12 | + client.topic=topic_query.get() |
| 13 | + if(client.topic=="Select topic"): |
| 14 | + client.topic = "top stories" |
| 15 | + |
| 16 | + client.language=language_query.get() |
| 17 | + if(client.language=="Select language"): |
| 18 | + client.language="english" |
| 19 | + client.edition=edition_query.get() |
| 20 | + if(client.edition == "Select edition"): |
| 21 | + client.edition = "United States (English)" |
| 22 | + |
| 23 | + client.location= location_query.get() |
| 24 | + if(client.location==""): |
| 25 | + client.location = None |
| 26 | + |
| 27 | + update_status(client,status) |
| 28 | + for i in client.get_news(): |
| 29 | + print(i) |
| 30 | + print("\n") |
| 31 | + return |
| 32 | + |
| 33 | + |
| 34 | +# to search news related to a search string |
| 35 | +def search(): |
| 36 | + global search_query,client,status |
| 37 | + client.query= search_query.get() |
| 38 | + update_status(client,status) |
| 39 | + print(client.get_news()) |
| 40 | + return |
| 41 | + |
| 42 | +# helper function for status bar |
| 43 | +def getstatus(clientX): |
| 44 | + x = clientX.get_config() |
| 45 | + y=sorted(x) |
| 46 | + text = "" |
| 47 | + for i in y: |
| 48 | + text += str(i) + ": " + str(x[i]) + " " |
| 49 | + return text |
| 50 | + |
| 51 | +# set status bar text |
| 52 | +def update_status(client, status): |
| 53 | + status['text']=getstatus(client) |
| 54 | + return |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +#initializing window |
| 59 | +root = Tk() |
| 60 | +root.title("GNEWSCLIENT") |
| 61 | + |
| 62 | +#object making and packing |
| 63 | +client = gnewsclient() |
| 64 | +status = Label(root, text=getstatus(client), bd=1, relief=SUNKEN) |
| 65 | +status.pack(side=BOTTOM, fill=X) |
| 66 | + |
| 67 | + |
| 68 | +# implementing searching news |
| 69 | +search_frame=Frame(root) |
| 70 | +search_frame.pack() |
| 71 | +search_query = StringVar() |
| 72 | +query = Entry(search_frame, textvariable=search_query).grid(row=1, column=0) |
| 73 | +search_button = Button(search_frame,text= "search", command=search).grid(row=1, column = 1) |
| 74 | + |
| 75 | +west_frame = Frame(root) |
| 76 | +west_frame.pack(side=LEFT) |
| 77 | + |
| 78 | + |
| 79 | +# frame for topic, location, and edition filters |
| 80 | +tle_frame=Frame(west_frame) |
| 81 | +tle_frame.pack() |
| 82 | + |
| 83 | +# implementing edition filter |
| 84 | +edition_label = Label(tle_frame, text="Edition: ").grid(row=1, column=0,pady=50,sticky=W) |
| 85 | +edition_query=StringVar() |
| 86 | +edition_dropdown = ttk.Combobox(tle_frame, textvariable=edition_query, values=sorted(list(utils.editionMap))).grid(row=1, column=1,pady=50) |
| 87 | +edition_query.set("Select edition") |
| 88 | + |
| 89 | +# implementing topic filter |
| 90 | +topic_label = Label(tle_frame, text="Topic: ").grid(row=2, column=0,pady=50,sticky=W) |
| 91 | +topic_query=StringVar() |
| 92 | +topic_dropdown = ttk.Combobox(tle_frame, textvariable=topic_query, values=sorted(list(utils.topicMap))).grid(row=2, column=1,pady=50) |
| 93 | +topic_query.set("Select topic") |
| 94 | + |
| 95 | + |
| 96 | +# implementing language filter |
| 97 | +language_label = Label(tle_frame, text="Language: ").grid(row=3, column=0,pady=50,sticky=W) |
| 98 | +language_query=StringVar() |
| 99 | +language_dropdown = ttk.Combobox(tle_frame, textvariable=language_query, values=sorted(list(utils.langMap))).grid(row=3, column=1,pady=50) |
| 100 | +language_query.set("Select language") |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +# implementing location filter |
| 105 | +location_label= Label(tle_frame,text="Location: ").grid(row=4,column=0,pady=50,sticky=W) |
| 106 | +location_query = StringVar() |
| 107 | +location = Entry(tle_frame, textvariable=location_query).grid(row=4, column=1,pady=50) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +# button to fetch news |
| 112 | +getnews = Button(tle_frame, command=news,text = "Get News!").grid(row=6,column=0,columnspan=2, pady=25) |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +root.mainloop() |
0 commit comments