-1

The code I have attached below functions like this: It takes users ID and marks. It goes through an excel sheet and finds that specific ID. Then it will update the second column in that row with his marks. However, this code gives me an error when I run it. The error comes from .get() function used for getting value of an Entry. I have used .get() function in other projects where it works.

import xlwt
import xlrd
from xlutils.copy import copy
from tkinter import *
root = Tk()
root.title("Quiz marks uploader")
root.geometry("500x500")
rnn = "global"
maar = "global"

def upload():
    rn = entry_1.get()
    mar = entry_2.get()
    rnn = rn
    maar = mar

button_1 = Button(root, text = "Upload", command = upload).place(relx = 0.3,rely = 0.2,anchor = NE)
label_1 = Label(root, text = "Enter Reg No here").place(relx = 0.2,rely = 0.05,anchor = E)
entry_1 = Entry(root).place(relx = 0.5,rely = 0.05,anchor = E)
label_2 = Label(root, text = "Enter marks here").place(relx = 0.2,rely = 0.1,anchor = E)
entry_2 = Entry(root).place(relx = 0.5,rely = 0.1,anchor = E)

workbook = xlrd.open_workbook("file.xls")
sheet = workbook.sheet_by_index(0) 
rb = xlrd.open_workbook("file.xls")
wb = copy(rb)
w_sheet = wb.get_sheet(0)
for row in range(sheet.nrows): 
    row_value = sheet.row_values(row)
    if row_value[0] == rnn:
        w_sheet.write(row,1,maar)
        print (row_value)

wb.save("file.xls")
root.mainloop()
cassini
  • 21
  • 1
  • 4

2 Answers2

0

This is where you are going wrong:

You are doing:

entry_1 = Entry(root).place(relx = 0.5,rely = 0.05,anchor = E)

In this case, the entry_1 is a NoneType object.

And NoneType object doesn't have any property get(), therefore it throws an error.

What you should do:

First instantiate an Entry widget and then give your widget a position in the window using the widget reference.

entry_1 = Entry(root)
entry_1.place(relx = 0.5,rely = 0.05,anchor = E)

Updated code:

import xlwt
import xlrd
from xlutils.copy import copy
from tkinter import *
root = Tk()
root.title("Quiz marks uploader")
root.geometry("500x500")
rnn = "global"
maar = "global"

def upload():
    rn = entry_1.get()
    mar = entry_2.get()
    rnn = rn
    maar = mar

button_1 = Button(root, text = "Upload", command = upload).place(relx = 0.3,rely = 0.2,anchor = NE)

label_1 = Label(root, text = "Enter Reg No here").place(relx = 0.2,rely = 0.05,anchor = E)

# entry widget 1
entry_1 = Entry(root)
entry_1.place(relx = 0.5,rely = 0.05,anchor = E)

label_2 = Label(root, text = "Enter marks here").place(relx = 0.2,rely = 0.1,anchor = E)

# entry widget 2
entry_2 = Entry(root)
entry_2.place(relx = 0.5,rely = 0.1,anchor = E)

workbook = xlrd.open_workbook("file.xls")
sheet = workbook.sheet_by_index(0) 
rb = xlrd.open_workbook("file.xls")
wb = copy(rb)
w_sheet = wb.get_sheet(0)
for row in range(sheet.nrows): 
    row_value = sheet.row_values(row)
    if row_value[0] == rnn:
        w_sheet.write(row,1,maar)
        print (row_value)

wb.save("file.xls")
root.mainloop()

Hope it helps!

Somraj Chowdhury
  • 983
  • 1
  • 6
  • 14
0

Instead of xlrd use pandas for managing excel files.

To create simple excel file:

import pandas as pd

df = (pd.DataFrame({'ID': [10, 11, 12], 'Mark': ['ww','zz','cc']}))
df.to_excel('data.xlsx')

I add some changes in elements positions. Now you can update data like below:

import pandas as pd
from tkinter import *
root = Tk()
root.title("Quiz marks uploader")
root.geometry("500x500")


def upload():
    id = entry_id.get()
    mark = entry_mark.get()
    # load excel into data frame
    df = pd.read_excel('data.xlsx', index_col=0)

    # find proper row and replace value
    row_match = df[df['ID'] == int(id)]
    if row_match.shape[0] > 0:
        df.at[row_match.index[0], 'Mark'] = mark

    # save updated file
    df.to_excel('data.xlsx')


# set button and labels
button = Button(root, text = "Upload", command=upload)
button.grid(column=1, row=4)
label = Label(root, text = "Id")
label.grid(column=0, row=2)
entry_id = Entry(root)
entry_id.grid(column=1, row=2)
label = Label(root, text = "Mark")
label.grid(column=0, row=3)
entry_mark = Entry(root)
entry_mark.grid(column=1, row=3)

root.mainloop()
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38