简体   繁体   English

Python 中的 Filedialog 问题,Selenium 'NameError'

[英]Problem with Filedialog in Python, Selenium 'NameError'

I work on this Projekt where I try to get Links from a Website and I got it to work after 2-3 Weeks and now my Task is to build an GUI with TKinter.我在这个项目上工作,我尝试从网站获取链接,并在 2-3 周后让它工作,现在我的任务是用 TKinter 构建一个 GUI。 The Problem is that I ask the User to select the Driver that is needed for the Selenium Operation, that grabs the Links from the Website问题是我要求用户选择 Selenium 操作所需的驱动程序,从网站上获取链接

So this is my Code and probably the Problem is easy but I'm just stuck at the part from coderetter() to code() where I give the Filepath to the Driver it always says: "NameError: name 'save' is not defined" and I tried everything The Problem occurs on the last line by executable_path that where i want to let the User choose the path.所以这是我的代码,可能问题很简单,但我只是停留在从 coderetter() 到 code() 的部分,在那里我将文件路径提供给驱动程序它总是说:“NameError:name 'save' is not defined "并且我尝试了所有问题发生在最后一行的executable_path,我想让用户选择路径。

Has anyone got any similar kind of problem or does someone maybe see the problem here?有没有人遇到过类似的问题,或者有人可能在这里看到了这个问题?

import csv
from tkinter import *
from tkinter import filedialog
from multiprocessing import Process

def Fenster():

    root = Tk()
    root.title("MS-Search Grabber")
    w = Label(root, text='W!')

    driverButt = Button(text='Chrome-Driver', command=coderetter)
    FileSave = Button(text='Save CSV', command=saveFile)
    StartB = Button(text='Start Process', command=proSt)
    w.pack()
    driverButt.pack()
    FileSave.pack()
    StartB.pack()
    root.mainloop()

def coderetter():

    file = filedialog.askopenfilename()



def saveFile():

    save = filedialog.asksaveasfile()
    return save

def proSt():
    p2 = Process(target=code)
    p2.start()


def code():

    why = saveFile(save)
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('headless')
    chrome_options.add_argument('window-size=1920x1080')
    chrome_options.add_argument('disable-gpu')


    driver = webdriver.Chrome(executable_path=why, options=chrome_options)

if __name__ == "__main__":
    file = ''
    driver= ''
    save = ''
    p1 = Process(target=Fenster)
    p1.start()

The use of global will enable file and save strings to be available to other parts of your code.使用global将使文件和保存字符串可用于代码的其他部分。 By using global in saveFile, you do not need a return This means Filesave button will also function correctly since buttons do not accept returned information.通过在 saveFile 中使用 global,您不需要返回这意味着 Filesave 按钮也将正常运行,因为按钮不接受返回的信息。

def coderetter():
    global file
    file = filedialog.askopenfilename()

def saveFile():
    global save
    save = filedialog.asksaveasfile()

def proSt():
    p2 = Process(target=code)
    p2.start()

def code():
    saveFile()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM