简体   繁体   中英

Python 3 - Tkinter nameError

NameError: name 'onOpen' is not defined

There is something wrong with the command function. I am not sure what I did wrong here. I had the code tested before the onOpen function and it works fine.

    import tkinter as tk
from tkinter import filedialog

class Application(tk.Frame): 
    def __init__(self, master=None):
        tk.Frame.__init__(self, master) 
        self.pack() 
        self.createWidgets()

    def onOpen():
        """ Ask the user to choose a file and update the values of label change button"""
        button_label.set(filedialog.askopenfilename(filetypes = ()))

    def createWidgets(self):

        #instruction label
        self.labelInstruct = tk.Label(self, text="Instructions:", padx=10, pady=10)
        self.labelInstruct = tk.Label(self, text="All you have to do is insert the file and save it.\n The conversion is instant", padx=10, pady=10)
        self.labelInstruct.pack()

        #insertfile button
        self.ifbut = tk.Button(self, text="Insert File", command=onOpen)
        self.ifbut.pack()

        button_label = tk.StringVar(self)
        text = tk.Label(self, textvariable = button_label)
        text.pack()

        #save button
        self.saveLabel = tk.Label(self, text="Save File", padx=10, pady=10)
        self.saveLabel.pack()
        self.saveButton = tk.Button(self)
        self.saveButton.pack()

        #quit button
        self.quitButton = tk.Button(self, text='Quit',
            command=self.quit) 
        self.quitButton.pack()

app = Application() 
app.master.title('Sample application') 
app.mainloop()

Seeing that there are many problems with the way this code is written I am only going to point out a few of them and tackle the main question from the OP.

Lets start with the fact that you need to define the main window with something like root = tk.Tk() and you also need to make sure all your methods in your class have the argument self as the first argument.

Also any variable you are defining also should have self so that the class can interact with it. This ( button_label ) for example should be self.button_label .

There is no reason to use return the way you are trying to in the onOpen(self): method. Return does not work like that. Return is there so you can return a value to something that is calling the function/method to be used for something else and is not for setting the value of something.

Note that I also add the root window variable to the app = Application(root) line. This lets us pass the main window into the class.

all and all the following should work for the onOpen(self): function but the rest still needs some work.

import tkinter as tk
from tkinter import filedialog


class Application(tk.Frame): 
    def __init__(self, parent):
        tk.Frame.__init__(self, parent) 
        self.parent = parent
        self.pack() 
        self.createWidgets()

    def onOpen(self):
        """ Ask the user to choose a file and update the values of label change button"""
        self.button_label.set(filedialog.askopenfilename(filetypes = ()))

    def createWidgets(self):

        #instruction label
        self.labelInstruct = tk.Label(self.parent, text="Instructions:", padx=10, pady=10)
        self.labelInstruct = tk.Label(self.parent, text="All you have to do is insert the file and save it.\n The conversion is instant", padx=10, pady=10)
        self.labelInstruct.pack()

        #insert file button
        self.ifbut = tk.Button(self.parent, text="Insert File", command = self.onOpen)
        self.ifbut.pack()

        self.button_label = tk.StringVar()
        self.text = tk.Label(self.parent, textvariable = self.button_label)
        self.text.pack()

        #save button
        self.saveLabel = tk.Label(self.parent, text="Save File", padx=10, pady=10)
        self.saveLabel.pack()
        self.saveButton = tk.Button(self.parent, text = "Save")
        self.saveButton.pack()

        #quit button
        self.quitButton = tk.Button(self.parent, text='Quit', command=self.quit) 
        self.quitButton.pack()


root = tk.Tk()
app = Application(root) 
app.master.title('Sample application') 
app.mainloop()

You need to return the function value as below:

def onOpen():
    """ Ask the user to choose a file and update the values of label change button"""
    return button_label.set(filedialog.askopenfilename(filetypes = ()))    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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