寻求帮助:python2.7:正在使用此函数;希望从“def proceed3()”获取到“def openChrome()”的输入

2024-06-02 06:14:56 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将我的网站名称传递到“def openChrome()”,例如,从“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()

回溯错误:

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

Tags: textnameinfalsetkinterwindowsdefwebsite
1条回答
网友
1楼 · 发布于 2024-06-02 06:14:56

添加 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)

我建议阅读this tutorial关于如何在python中使用函数的内容,因为这将是进一步进行编程工作的基础。你知道吗

相关问题 更多 >