-4

我如何传递我的website_name这是一个 URL,例如https://www.google.com/;从“defproceed3()”“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()

回溯错误:

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
4

1 回答 1

-1

添加 return website_name到您的proceed3()函数的末尾

website_name向您的 OpenChrome() 函数添加一个参数。

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 中使用函数,因为这将是您进一步编程工作的基础。

于 2017-06-19T21:59:40.207 回答