I am creating an app to help me learn python. This app consists of a tkinter gui that displays news data that is scraped from multiple sites which are selected in the gui. The user can also enter date, two text fields, and two drop downs to be passed to the backed.
My problem is that the updated values entered into the gui, e.g. description text are not registering to the actual variables (variable.get()) i.e. the variables in the dictionary on the 4th last line are NULL.
If I hard code values in the dictionary, the dictionary passes without an issue.
I know my code is not the best but I am learning. Any help whatsoever is greatly appreciated because I have been stuck on this for a day and cannot find a solution anywhere online.
The issue is in the add button at the very bottom of my code.
from tkinter import *
from tkcalendar import *
import pandas as pd
from functools import partial
import datetime
from tkinter import ttk
class FrontEnd(object):
def __init__(self):
self.window = Tk()
self.window.geometry("1000x600")
self.enter_data()
self.buttons()
self.window.mainloop()
def enter_data(self):
"""Users will enter data here"""
#Category
self.cat_text = ttk.Label(self.window, width = 10, text = "Category")
self.cat_text.grid(row=5,column=0,rowspan=1,columnspan=1,sticky=W)
#list for category drop down
self.OptionList = [
"News"
, "Alert"
, "Recall"
, "Discussion"
]
self.variable = StringVar(self.window)
self.variable.set(self.OptionList[0]) # default value
self.w = ttk.OptionMenu(self.window, self.variable, *self.OptionList).grid(row=6,column=0,rowspan=1,sticky=W)
#Date
self.date_text = ttk.Label(self.window, width = 8, text = "Date")
self.date_text.grid(row=5,column=1,rowspan=1,columnspan=1,sticky=W)
#Enter date
self.date_picker = DateEntry(self.window)
self.date_picker.grid(row=6,column=1,rowspan=1,sticky=W)
#Sources
self.source_list = ["EC RASFF","IFSQN","FDA","FSAI","NZ FSA","UK FSA","USDA","USDA FSIS","IFS","FSSC","SF360","BRC","FSANZ","SQF","EFSA","UN FAO","CFIA","FDA FSMA","UN FAO","EC RAPID","WHO"]
self.source_text = ttk.Label(self.window, width = 8, text = "Source")
self.source_text.grid(row=5,column=2,rowspan=1,columnspan=1,sticky=W)
self.source_variable = StringVar(self.window)
self.source_variable.set(self.source_list[0]) # default value
#Description
self.desc_text = ttk.Label(self.window, width = 12, text = "Description:")
self.desc_text.grid(row=8,column=0,rowspan=1,columnspan=1,sticky=W)
self.desc_tb = Text(self.window,height = 1, width = 80)
self.desc_tb.grid(row=8,column=1,rowspan=1,columnspan=6,sticky=W)
#URL
self.url_text = ttk.Label(self.window, width = 12, text = "URL:")
self.url_text.grid(row=9,column=0,rowspan=1,columnspan=1,sticky=W)
self.url_tb = Text(self.window,height = 1, width = 80)
self.url_tb.grid(row=9,column=1,rowspan=1,columnspan=6,sticky=W)
self.s = ttk.OptionMenu(self.window, self.source_variable, *self.source_list).grid(row=6,column=2,rowspan=1,sticky=W)
self.outp = Listbox(self.window)
self.outp.grid(row=12,column=0,rowspan=1,columnspan=30,sticky=N+E+W+S)
def buttons(self):
#Send the entered data from enter_data to the backend
self.addtodf = ttk.Button(self.window, width = 12, text = "Add"
, command = partial(self.output_window,
({"category": self.variable.get(), "description": self.desc_tb.get('1.0', END), "link": self.desc_tb.get('1.0', END), "date" : self.date_picker.get_date(), "site_type": self.source_variable.get()})
)).grid(row=3,column=5,rowspan=1,columnspan=1,sticky=W)
def output_window(self,dict):
self.outp.insert(END, dict)
self.outp.insert(END, "\n")
if __name__ == "__main__":
app = FrontEnd()
Full project is here: https://github.com/seangibs/FoodNewsWebScraper