Search code examples
pythonpython-3.xtkintertkinter-entry

Do i require DB to retain user-entered value from Tkinter GUI?


I was working with one of SEO tool and trying to create an GUI application where user will enter the website link. Then, my list_of_url() function will execute to find all the links present.

```python
def ui_for_website():
    app = Tk()
    app.title("Scrapper")
    app.geometry("300x300")
    
    ui_text=StringVar(app,name="str")
    ui_text_label=Label(app,text="Enter your website: ",font=('bold',12),pady=20)
    ui_text_label.grid(row=0,column=0)
    ui_text_entry=Entry(app,textvariable=ui_text)
    ui_text_entry.grid(row=0,column=1)
    #onclick=lambda : ui_text_entry.get()
    ui_text_button=Button(app,text="Submit",command=lambda : onClick(ui_text_entry,app))

    ui_text_button.grid(row=2,column=1)
    
    ui_text=ui_text_entry.get()
    print(ui_text)
    app.mainloop()
```

Here is my Tkinter UI code where i am asking user to enter.

```python
def onClick(entry,app):
    #global website_name
    website_name=entry.get()
    #print(website_name)
    #app.quit()
    return website_name
``

here is my onClick method which executes, once user clicks on the button "Submit". I am trying to return the value and by storing it in website_name variable.

```python
def list_of_url():
    
    url=ui_for_website()
    print(url)
    urls=session.get(url).html.absolute_links
    
    tree = sitemap_tree_for_homepage(url)
    #print(len(tree.all_pages()))
    #print(len(tree.pages))
    url_list=[]
    for page in tree.all_pages():
        if ('admin' not in page.url) and (page.url not in url_list):
            url_list.append(page.url)

    print(len(url_list))
    return url_list
```

This website_name, i am accessing it in this list_of_urls() method. But once, tkinter is closed. The value of this variable becomes null and i am unable to get the value.

Error which i got is

Traceback (most recent call last):
  File "Indexing.py", line 89, in <module>
    list_of_url()
  File "Indexing.py", line 42, in list_of_url
    urls=session.get(url).html.absolute_links
  File "C:\Users\<username>\Anaconda3\lib\site-packages\requests\sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\<username>\Anaconda3\lib\site-packages\requests\sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "C:\Users\<username>\Anaconda3\lib\site-packages\requests\sessions.py", line 462, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Users\<username>\Anaconda3\lib\site-packages\requests\models.py", line 313, in prepare
    self.prepare_url(url, params)
  File "C:\Users\<username>\Anaconda3\lib\site-packages\requests\models.py", line 387, in prepare_url
    raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None?

So, this makes me think if DB is required to retain user input value. So that even if the GUI is destroyed or closed. I will retain the user entered value.

Here is my complete code:

```python
from requests_html import HTMLSession
from tkinter import *

from usp.tree import sitemap_tree_for_homepage

session=HTMLSession()
#website_name=''

def onClick(entry,app):
    #global website_name
    website_name=entry.get()
    print(website_name)
    #app.quit()
    return website_name

def ui_for_website():
    app = Tk()
    app.title("Scrapper")
    app.geometry("300x300")
    
    ui_text=StringVar(app,name="str")
    ui_text_label=Label(app,text="Enter your website: ",font=('bold',12),pady=20)
    ui_text_label.grid(row=0,column=0)
    ui_text_entry=Entry(app,textvariable=ui_text)
    ui_text_entry.grid(row=0,column=1)
    #onclick=lambda : ui_text_entry.get()
    ui_text_button=Button(app,text="Submit",command=lambda : onClick(ui_text_entry,app))

    ui_text_button.grid(row=2,column=1)
    
    ui_text=ui_text_entry.get()
    print(ui_text)
    app.mainloop()

def list_of_url():
    
    url=ui_for_website()
    print(url)
    urls=session.get(url).html.absolute_links
    
    tree = sitemap_tree_for_homepage(url)
    #print(len(tree.all_pages()))
    #print(len(tree.pages))
    url_list=[]
    for page in tree.all_pages():
        if ('admin' not in page.url) and (page.url not in url_list):
            url_list.append(page.url)

    print(len(url_list))
    return url_list
```

Solution

  • Tkinter does not require connecting to a database. The error messages are about the request to http. To be more specific, it is coming from requests html library. No database is involved.