-1

I'm new to Python. I've tried to create a window using canvas (Tkinter), but it says button is not defined. Is button to be replaced with something else?

Error

Traceback (most recent call last):
  File "C:/Users/MY/Desktop/Courier-management/project.py", line 240, in <module>
    canvas.create_window(320, 60, window = button)
NameError: name 'button' is not defined
from tkinter import *
from tkinter import messagebox as ms
from tkinter import ttk
import sqlite3
import random

# Database 
with sqlite3.connect('Akash5.db') as db:
    c = db.cursor()
try:
    c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL ,password TEX  NOT NULL,mobile TEX NOT NULL);')
except:
    pass
db.commit()
db.close()

class main:
    def __init__(self,master): 
        self.master = master
        self.username = StringVar()
        self.password = StringVar()
        self.n_username = StringVar()
        self.n_password = StringVar()
        self.n_reg=StringVar()
        self.n_mobile=StringVar()
        self.mobile11=StringVar()
        self.widgets()

    def login(self):
        with sqlite3.connect('Akash5.db') as db:
            c = db.cursor()

        # Find user If there is any take proper action
        find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
        c.execute(find_user,[(self.username.get()),(self.password.get())])
        result = c.fetchall()

        if result:
            self.track()
        else:
            ms.showerror('Oops!','Username Not Found.')

    def new_user(self):
        with sqlite3.connect('Akash5.db') as db:
            c = db.cursor()
        if self.n_username.get()!=' ' and self.n_password.get()!=' ' and self.n_mobile.get()!=' ':
            find_user = ('SELECT * FROM user WHERE username = ?')
            c.execute(find_user,[(self.n_username.get())])        

            if c.fetchall():
                ms.showerror('Error!','Username Taken Try a Diffrent One.')
            else:
                insert = 'INSERT INTO user(username,password,mobile) VALUES(?,?,?)'
                c.execute(insert,[(self.n_username.get()),(self.n_password.get()),(self.n_mobile.get())])
                db.commit()

                ms.showinfo('Success!','Account Created!')
                self.log()
        else:
             ms.showerror('Error!','Please Enter the details.')

    def consignment(self):
        try:
            with sqlite3.connect('Akash5.db') as db:
                c = db.cursor()

            # Find user If there is any take proper action
            find_user = ('SELECT * FROM user WHERE mobile= ?')
            c.execute(find_user,[(self.mobile11.get())])
            result = c.fetchall()

            if result:
                self.track()
                self.crff.pack_forget()
                self.head['text'] = self.username.get() + '\n Your Product Details'
                self.consi.pack()
            else:
                ms.showerror('Oops!','Mobile Number Not Found.')
        except:
            ms.showerror('Oops!','Mobile Number Not Found.')

    def track1(self):
        self.consi.pack_forget()
        self.head['text'] = self.username.get() + '\n Track your Product'
        self.crff.pack()

    def log(self):
        self.username.set('')
        self.password.set('')
        self.crf.pack_forget()
        self.head['text'] = 'Login'
        self.logf.pack()

    def cr(self):
        self.n_username.set('')
        self.n_password.set('')
        self.logf.pack_forget()
        self.head['text'] = 'Create Account'
        self.crf.pack()

    def track(self):
        self.logf.pack_forget()
        self.head['text'] = self.username.get() + '\n Track your Product'
        self.crff.pack()

    def widgets(self):
        self.head = Label(self.master,text = 'LOGIN',font = ('',20),pady = 10)
        self.head.pack()

        self.logf = Frame(self.master,padx =10,pady = 10)
        self.logf.configure(background='lightblue')

        # PhotoImage(self.logf,file = 'lpu_logo.png')
        Label(self.logf,text = 'Username: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.username,bd = 3,font = ('',15)).grid(row=0,column=1)
        Label(self.logf,text = 'Password: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.password,bd = 3,font = ('',15),show = '*').grid(row=1,column=1)
        Button(self.logf,text = ' Login ',background='lightgrey',bd = 2 ,font = ('',13),padx=6,pady=6,command=self.login).grid(row=8,column=0)
        Button(self.logf,text = ' New user ',background='lightgrey',bd = 2 ,font = ('',13),padx=6,pady=6,command=self.cr).grid(row=8,column=1)

        self.logf.pack()

        self.crf = Frame(self.master,padx =10,pady = 10)
        Label(self.crf,text = 'Username: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,textvariable = self.n_username,bd = 3,font = ('',15)).grid(row=0,column=1)

        Label(self.crf,text = 'Password: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,textvariable = self.n_password,bd = 3,font = ('',15),show = '*').grid(row=1,column=1)

        Label(self.crf,text = 'Reg No.: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,textvariable = self.n_reg,bd = 3,font = ('',15)).grid(row=2,column=1)
        Label(self.crf,text = 'Gender: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        var = IntVar()
        R1 = Radiobutton(self.crf, text="Male", variable=var, value=1).grid(sticky=W)

        R2 = Radiobutton(self.crf, text="Female", variable=var, value=2 ).grid(row=4,column=1)
        Label(self.crf,text = 'Mobile No.: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,textvariable = self.n_mobile,bd = 3,font = ('',15)).grid(row=5,column=1)

        Label(self.crf,text = 'Email Id: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,bd = 3,font = ('',15)).grid(row=6,column=1)

        Button(self.crf,text = 'Create Account',background='lightgrey',bd = 2,font = ('',13),padx=6,pady=6,command=self.new_user).grid(row=11,column=0)
        Button(self.crf,text = 'Go to Login',background='lightgrey',bd = 2,font = ('',13),padx=6,pady=6,command=self.log).grid(row=11,column=1)

        self.crff = Frame(self.master,padx =10,pady = 10)

        Label(self.crff,text = 'Consignment No: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crff,bd = 3,font = ('',15)).grid(row=0,column=1)
        Label(self.crff,text = 'Mobile no:',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Entry(self.crff,bd = 3,textvariable = self.mobile11,font = ('',15)).grid(row=1,column=1)
        Button(self.crff,text = 'Track',background='lightgrey',bd = 2,font = ('',13),padx=6,pady=6,command=self.consignment).grid(row=4,column=0)

        self.consi = Frame(self.master,padx =10,pady = 10)

        Label(self.consi,text = ' Product ID:',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Label(self.consi,text =random.randint(565154,99994216) ,font = ('',13),pady=5,padx=5).grid(row=0,column=1)
        L = ['Bag','Colgate','shoe','Redme 2','Jeans','Parrot','Mac','Ipad','Pen','Book','shirt']
        f=random.randint(0,10)
        Label(self.consi,text = 'Product name: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Label(self.consi,text =L[f] ,font = ('',13),pady=5,padx=5).grid(row=1,column=1)
        Label(self.consi,text = 'Product Status: ',font = ('',15),pady=5,padx=5).grid(sticky = W)
        Label(self.consi,text ='Pending' ,font = ('',13),pady=5,padx=5).grid(row=2,column=1)
        Label(self.consi,font = ('',13), text = 'Thanks for Exploring!').grid(row = 4, column = 0)

        Label(self.consi, text = 'Comments:',font = ('',13)).grid(row = 5, column = 0, padx = 5, sticky = 'sw')
        Entry(self.consi,bd = 3,font = ('',15)).grid(row=5,column=1)

        Button(self.consi,text = 'Back',background='lightgrey',bd = 2,font = ('',13),padx=6,pady=6,command=self.track1).grid(row=6,column=0)  

if __name__ == '__main__':
    root = Tk()
    root.title('Track Consignment')
    root.geometry('800x750+300+300')
    main(root)

    canvas = Canvas(root)
    canvas.pack()
    canvas.config(width = 640, height = 380)

    line = canvas.create_line(160, 360, 480, 120, fill = 'blue', width = 5)
    canvas.itemconfigure(line, fill = '#1abc9c')
    print(canvas.coords(line))
    canvas.coords(line, 0, 0, 320, 240, 640, 0)

    canvas.itemconfigure(line, smooth = True)
    canvas.itemconfigure(line, splinesteps = 5)
    canvas.itemconfigure(line, splinesteps = 100)
    canvas.delete(line)

    rect = canvas.create_rectangle(160, 120, 480, 360)
    canvas.itemconfigure(rect, fill = '#3498db')
    oval = canvas.create_oval(160, 120, 480, 360)
    arc = canvas.create_arc(160, 1, 480, 240)
    canvas.itemconfigure(arc, start = 0, extent = 180, fill = '#1abc9c')
    poly = canvas.create_polygon(160, 360, 320, 480, 480, 360, fill = '#95a5a6')
    text = canvas.create_text(320, 240, text = 'Python', font = ('Courier', 32, 'bold'))

    logo = PhotoImage(file = 'logo1.gif') 
    image = canvas.create_image(320, 240, image = logo)

    canvas.lift(text)
    canvas.lower(image)
    canvas.lower(image, text)

    canvas.create_window(320, 60, window = button)

    canvas.itemconfigure(rect, tags = ('shape'))
    canvas.itemconfigure(oval, tags = ('shape', 'round'))
    canvas.itemconfigure('shape', fill = 'grey')
    print(canvas.gettags(oval))
    root.mainloop()
2
  • 2
    I don't see button defined anywhere.
    – brunns
    Apr 19, 2019 at 16:39
  • 1
    What do you think button is? In line canvas.create_window(320, 60, window = button). You probably forget to define button
    – Saad
    Apr 19, 2019 at 16:44

2 Answers 2

0

On line 240 "button" is not a defined variable name. Perhaps you meant to create a new button called "button" first to add in?

Or possibly you meant to use one of your existing buttons which you have already created such as R1 or R2.

"button" may make sense to us as when we see "button" we think of a button but to python "button" is not defined as anything, and therefore it does not know what it is, what it means, or what to do with it.

2
  • if I still call R1 and R2 it will again show undefined I guess
    – Mht K
    Apr 19, 2019 at 16:48
  • If your goal is to pass a certain button there which is uncreated, then I would create a new button as is necessary, then pass that newly created button in. Apr 19, 2019 at 17:02
0

R1 and R2 are localized to the widgets function. They cannot be accessed anywhere else unless you declare them in a global scope AND prefix them with "global " in the function you want to access them from. Furthermore, I think you mean to assign like this:

Perhaps you meant to use poly instead? It seems unused so far:

  canvas.create_window(320, 60, window = poly)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.