所以我对Python编程非常陌生,作为学习的一部分,我尝试使用tkinter编写一个.exe程序,要求用户插入地址,然后脚本将在OpenStreetMap上移至该位置。
如果我跳过tkinter并仅通过控制台运行,并且使用tkinter编写了另一个可以正常工作的小程序,则代码本身可以工作。但是,当我尝试使用tkinter运行代码时,用户输入仅返回:u“。!entry”。
例如,如果用户插入“英国伦敦,伦敦市”,我希望该条目进入该行:query = f'u“ {Go2LocationFinal}”',但是我得到的不是上面的输入“。!entry”错误,而我要读取的是:query = u。“英国伦敦,伦敦市”
import webbrowser
from opencage.geocoder import OpenCageGeocode`
from pprint import pprint
import tkinter as tk
from ipyleaflet import*
key = 'my_key'
geocoder = OpenCageGeocode(key)
class App(tk.Frame):
def __init__(self,master):
self.master = master
tk.Label(self.master, text = "Go to address:").grid(row = 0)
self.Go2Location = tk.Entry(self.master, width = 10)
self.Go2Location.grid(row = 0, column = 1)
btn1 = tk.Button(self.master, text = "Submit entry", command = self.go_2_google)
btn1.grid(row = 3, column = 1)
def go_2_google(self):
Go2LocationExecute = self.Go2Location
Go2LocationFinal = str(Go2LocationExecute)
query = f'u"{Go2LocationFinal}"'
results = geocoder.geocode(query)
pprint(u'%f;%f;%s;%s' % (results[0]['geometry']['lat'],
results[0]['geometry']['lng'],
results[0]['components']['country_code'],
results[0]['annotations']['timezone']['name']))
x = results[0]['geometry']['lat']
y = results[0]['geometry']['lng']
x1 = float(x)
y1 = float(y)
webbrowser.open(f"https://www.openstreetmap.org/#map=16/{x1}/{y1}")
if __name__ == "__main__":
root = tk.Tk()
myapp = App(root)
root.mainloop()