如何传递我的 website_name ,这是一个网址,例如https://www.google.com/;从" def proceed3()" 到" def openChrome()" ?有什么建议吗?
from Tkinter import *
def proceed3():
popup = Toplevel()
popup.geometry("350x175+350+180")
popup.resizable(width=False, height=False)
instruction = Label(popup, text="Enter the URL and pressGO!").pack()
website_name = Entry(popup, width=50).pack(pady=5)
goButton = Button(popup, text="GO!", command=openChrome)
goButton.pack(pady=5)
def openChrome():
openWebsite = website_name.get()
os.system(r"start chrome " + openWebsite)
windows = Tk()
windows.geometry("200x200+375+280")
windows.resizable(width=False, height=False)
submitButton = Button(windows, text='OpenChrome', command=proceed3)
submitButton.pack(pady=5)
windows.mainloop()
TRACEBACK ERROR:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "E:/educational_data/PyCharmProjects/web_grabber/starflow_grabber.py",
line 15, in openChrome
openWebsite = website_name.get()
NameError: global name 'website_name' is not defined
答案 0 :(得分:-1)
添加
return website_name
到您的proceed3()函数的末尾
在OpenChrome()函数中添加一个参数website_name
。
def proceed3():
popup = Toplevel()
popup.geometry("350x175+350+180")
popup.resizable(width=False, height=False)
instruction = Label(popup, text="Enter the URL and pressGO!").pack()
website_name = Entry(popup, width=50).pack(pady=5)
goButton = Button(popup, text="GO!", command=openChrome)
goButton.pack(pady=5)
return website_name
def openChrome(website_name):
os.system(r"start chrome " + website_name)
我建议阅读有关如何在python中使用函数的this tutorial,因为这对于进一步完善编程工作至关重要。