-1

I have a code to insert data on an excel file from a tkinter GUI. After I submit the data, the text is still showing on the entry and i want to automatically clear the text or to assign it to a button. I tried this How to clear the Entry widget after a button is pressed in Tkinter? but with no success

from datetime import date, datetime
from tkinter import *
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
import os

import openpyxl

wb = openpyxl.load_workbook('excel.xlsx')  # selectie fisier
sheet = wb["Productie"]  # selectie Fila
weekNumber = date.today().isocalendar()[1]  # cautare numarul saptamanii

root = Tk()
root.title("Main Menu")

def write_to_xlsx():
    model = e.get()
    etapa = e2.get()
    batch = e3.get()
    qty = e4.get()
    sn1 = e5.get()

    ws = wb.active
    maxim = ws.max_row + 1  # Definire capat de lista
    # Imbinare celule
    ws.merge_cells(start_row=maxim, start_column=1, end_row=maxim + 1, end_column=1)  # A
    ws.merge_cells(start_row=maxim, start_column=2, end_row=maxim + 1, end_column=2)  # B
    ws.merge_cells(start_row=maxim, start_column=3, end_row=maxim + 1, end_column=3)  # C
    ws.merge_cells(start_row=maxim, start_column=4, end_row=maxim + 1, end_column=4)  # D
    ws.merge_cells(start_row=maxim, start_column=5, end_row=maxim + 1, end_column=5)  # E
    ws.merge_cells(start_row=maxim, start_column=6, end_row=maxim + 1, end_column=6)  # F
    ws.merge_cells(start_row=maxim, start_column=7, end_row=maxim + 1, end_column=7)  # G
    ws.merge_cells(start_row=maxim, start_column=8, end_row=maxim + 1, end_column=8)  # H
    ws.merge_cells(start_row=maxim, start_column=24, end_row=maxim + 1, end_column=24)  # X
    ws.merge_cells(start_row=maxim, start_column=25, end_row=maxim + 1, end_column=25)  # Y
    # Introducere formule
    weekNumber = date.today().isocalendar()[1]  # cautare numarul saptamanii
    ws.cell(column=1, row=maxim, value=weekNumber)  # A-Week

    now = datetime.now()  # importare data si ora
    dt_string = now.strftime('%d/%m')  # formatare sa arate doar ziua si luna
    ws.cell(column=2, row=maxim, value=dt_string)  # B zi/luna
    from openpyxl.styles import Alignment

    ws.cell(column=3, row=maxim, value=model)

    ws.cell(column=4, row=maxim, value=etapa)

    ws.cell(column=5, row=maxim, value=batch)

    ws.cell(column=6, row=maxim, value=int(qty))

    ws.cell(column=7, row=maxim, value=int(sn1))

    ws.cell(column=8, row=maxim, value=int(sn1 + qty))

    rows = range(1, maxim + 1)
    columns = range(1, 8)
    for row in rows:
        for col in columns:
            sheet.cell(row, col).alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)

    wb.save("excel.xlsx")


global e2

e = Entry(root, width=20)
e.grid(row=0, column=1, sticky=W, padx=15)
l = Label(root, text="Model")
l.grid(row=0, column=0, sticky=W, padx=15)

e2 = Entry(root, width=20)
e2.grid(row=1, column=1, sticky=W, padx=15)
modela = Label(root, text="Etapa")
modela.grid(row=1, column=0, sticky=W, padx=15)

e3 = Entry(root, width=20)
e3.grid(row=2, column=1, sticky=W, padx=15)
model1 = Label(root, text="Batch")
model1.grid(row=2, column=0, sticky=W, padx=15)

e4 = Entry(root, width=20)
e4.grid(row=3, column=1, sticky=W, padx=15)
model2 = Label(root, text="Cantitate")
model2.grid(row=3, column=0, sticky=W, padx=15)

e5 = Entry(root, width=20)
e5.grid(row=4, column=1, sticky=W, padx=15)
model3 = Label(root, text="S/N")
model3.grid(row=4, column=0, sticky=W, padx=15)

submit = Button(root, text="Submit", command=write_to_xlsx)
submit.grid(row=1, column=2, sticky=E, padx=10)

root.mainloop()
  • What does "no success" mean? What did it do? This code doesn't seem to show any attempt to clear the entry widgets. – Bryan Oakley Feb 21 '20 at 20:32

3 Answers3

0

This worked fine for me:

def clear_text():
    e.delete(0, 'end')
    e2.delete(0, 'end')
    e3.delete(0, 'end')
    e4.delete(0, 'end')
    e5.delete(0, 'end')
John
  • 69
  • 3
0

You need to add delete(0, END) for each entry in you executed command after button click. Example below:

from tkinter import *

root = Tk()
def write_to_xlsx():
    e.delete(0, END)

e = Entry(root, width=20)
e.grid(row=4, column=1, sticky=W, padx=15)
submit = Button(root, text="Submit", command=write_to_xlsx)
submit.grid(row=1, column=2, sticky=E, padx=10)
root.mainloop()
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
0

As the others have pointed out delete(o, 'end') is what you are looking for however I think you would also benefit from learning how to dynamically generate your entry fields to save on writing so many lines of code.

  1. All imports should go at the top. Not in functions. As it is now you are importing Alignment every time the button is pressed instead of just once at the start of the code.

  2. global e2 is not doing what you think it is. As it stands now this does absolutely nothing for you. Global is used to tell a function that a defined value exist in the global namespace but this must be done in the function to work not outside of it.

  3. Instead of from tkinter import * use import tkinter as tk. This will help prevent overwriting of method.

  4. Rewrite your code to loop through a range to build your entry fields. This can be done with a combination of a list or 2 and a for loop and will make your code easier to read.

  5. The main part of your question. To delete everything in an entry field you want to do entry.delete(o, 'end).

Cleaned up code. Let me know if you have any questions:

import tkinter as tk
from datetime import date, datetime
from openpyxl.styles import Alignment
import openpyxl

wb = openpyxl.load_workbook('excel.xlsx')
sheet = wb["Productie"]
week_number = date.today().isocalendar()[1]

root = tk.Tk()
root.title("Main Menu")


def write_to_xlsx():
    ws = wb.active
    maxim = ws.max_row + 1

    for i in range(8):
        ws.merge_cells(start_row=maxim, start_column=i+1, end_row=maxim + 1, end_column=i+1)  # A

    ws.merge_cells(start_row=maxim, start_column=24, end_row=maxim + 1, end_column=24)  # X
    ws.merge_cells(start_row=maxim, start_column=25, end_row=maxim + 1, end_column=25)  # Y
    week_number = date.today().isocalendar()[1]
    ws.cell(column=1, row=maxim, value=week_number)

    now = datetime.now()
    dt_string = now.strftime('%d/%m')
    ws.cell(column=2, row=maxim, value=dt_string)

    for ndex, entry in enumerate(entry_list):
        ws.cell(column=ndex+3, row=maxim, value=entry_list[ndex].get())
        entry_list[ndex].delete(0, 'end')

    rows = range(1, maxim + 1)
    columns = range(1, 8)
    for row in rows:
        for col in columns:
            sheet.cell(row, col).alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)

    wb.save("excel.xlsx")


label_list = ['Model', 'Etapa', 'Batch', 'Cantitate', 'S/N']
entry_list = []

for i in range(len(label_list)):
    tk.Label(root, text=label_list[i]).grid(row=i, column=0, sticky='w', padx=15)
    entry_list.append(tk.Entry(root, width=20))
    entry_list[-1].grid(row=i, column=1, sticky='w', padx=15)

tk.Button(root, text='Submit', command=write_to_xlsx).grid(row=1, column=2, sticky='e', padx=10)
root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Thank you, i know that the code is a mess, i only started learning python 3 weeks ago. Your comment was very helpful – Filip Adrian Feb 21 '20 at 22:07