Teerath
Teerath

Reputation: 45

How to convert a local variable into a global variable?

This is a snippet of my code. I am trying to convert the local variable (choice) into a global variable so I can use the value it is holding later in the code, but I am unsure to how to do this. The code has no errors.

from tkinter import ttk
from tkinter import messagebox
from tkinter import Tk

root = Tk()
root.geometry("400x400")
cmb = ttk.Combobox(root, width="10",
values=("us","mexico","uk","france"))
cmb.place(relx="0.1",rely="0.1")

def checkcmbo():
    choice = cmb.get()

btn = ttk.Button(root, text="Get Value",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")

URL='https://www.worldometers.info/coronavirus/country/'+choice+'/'
#this is where I need to use the value in the local variable

root.mainloop()

#Web scrapes the data containing the dates and daily deaths
URL='https://www.worldometers.info/coronavirus/country/'+choice+'/'
#### This is where I need to use the value in the local variable ####
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results=soup.find_all("div", {"class": "col-md-12"})
data=results[4]
script = data.find('script')
string = script.string

Upvotes: 0

Views: 1453

Answers (3)

imxitiz
imxitiz

Reputation: 3987

I believe you do this :

from tkinter import ttk
from tkinter import messagebox
from tkinter import Tk

root = Tk()
root.geometry("400x400")
cmb = ttk.Combobox(root, width="10", 
values=("us","mexico","uk","france"))
cmb.place(relx="0.1",rely="0.1")

def checkcmbo():
    URL='https://www.worldometers.info/coronavirus/country/'+cmb.get()+'/'
    page = requests.get(URL)
    soup = BeautifulSoup(page.content, "html.parser")
    results=soup.find_all("div", {"class": "col-md-12"})
    data=results[4]
    script = data.find('script')
    string = script.string

btn = ttk.Button(root, text="Get Value",command=checkcmbo)
btn.pack()

root.mainloop()

I hadn't saw your complete code, so I can't be sure but this should solve your problem.

Upvotes: 1

user15801675
user15801675

Reputation:

I suggest you should go with textvariables or StringVar(). To get its value, use .get() and tonset its value, use .set('...')

from tkinter import Tk, StringVar

...
cmb1=StringVar()
cmb = ttk.Combobox(root, width="10",textvariable=cmb1,
values=("us","mexico","uk","france")
cmb.place(relx="0.1",rely="0.1")

def checkcmbo():
    print(cmb1.get())

btn = ttk.Button(root, text="Get Value",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")

....

Upvotes: 0

pu239
pu239

Reputation: 748

Change the definition of choice to global (keep it outside the function) and
Define your function like this:

choice = None # or any random initial value
def checkcmbo():
    global choice
    choice = cmb.get()
    print(choice)

The use of global tells python that the global variable choice can be edited inside that function. However, if you just wanted to access a global variable (not edit it) you don't need to use the global keyword.

Upvotes: 0

Related Questions