如何将局部变量转换为全局变量?
How to convert a local variable into a global variable?
这是我的代码片段。我正在尝试将局部变量 (choice
) 转换为全局变量,以便稍后在代码中使用它所持有的值,但我不确定如何执行此操作。代码没有错误。
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
将choice
的定义改为全局(保留在函数外)和
像这样定义你的函数:
choice = None # or any random initial value
def checkcmbo():
global choice
choice = cmb.get()
print(choice)
global
的使用告诉 python 可以在该函数内编辑全局变量 choice
。
但是,如果您只想访问全局变量(而不是编辑它),则不需要使用 global
关键字。
我建议您使用文本变量或 StringVar()
。要获取其值,请使用 .get()
并设置其值,请使用 .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")
....
我相信你会这样做:
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()
我没有看到你的完整代码,所以我不能确定,但这应该可以解决你的问题。
这是我的代码片段。我正在尝试将局部变量 (choice
) 转换为全局变量,以便稍后在代码中使用它所持有的值,但我不确定如何执行此操作。代码没有错误。
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
将choice
的定义改为全局(保留在函数外)和
像这样定义你的函数:
choice = None # or any random initial value
def checkcmbo():
global choice
choice = cmb.get()
print(choice)
global
的使用告诉 python 可以在该函数内编辑全局变量 choice
。
但是,如果您只想访问全局变量(而不是编辑它),则不需要使用 global
关键字。
我建议您使用文本变量或 StringVar()
。要获取其值,请使用 .get()
并设置其值,请使用 .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")
....
我相信你会这样做:
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()
我没有看到你的完整代码,所以我不能确定,但这应该可以解决你的问题。