0
votes

This is the class I'm calling and the function from a different file

class CalcFunc:

    def clearScreen(self):
        self.log("CLEAR (CE)")
        ent.delete(0, END)

This is the Entry Box

ent = Entry(root, textvariable=clc.getBtn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
ent.grid(row=0, columnspan=3, pady=10)

This is the button I'm clicking to clear the Entry Box

buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)

I'm not sure what the syntax is to be able to to clear an Entry widget from a class basically. That code worked when I had it in the same file but my project requires it to be in a separate file. It's a class project for a calculator and the "clear" button clears the Entry widget. I can post my entire code if that helps. Thank you.

----EDIT----

My Class

import time

class CalcFunc:

    def log(self, val):
        myFile = open(r".\log.dat", "a")
        myFile.write("%s\n" % val)
        myFile.close()

    def onScreen(self, iVal):
        self.log(iVal)
        currentTxt = self.getBtn.get()
        updateEnt = self.getBtn.set(currentTxt + iVal)    

    def clearScreen(self):
        self.log("CLEAR (CE)")
        ent.delete(0, END)

    def evaL(self):
        self.log("=")
        self.getBtn.set(str(eval(self.getBtn.get())))
        self.log(self.getBtn.get())

    def logLbl(self):
        myFile = open(r".\log.dat", "a")
        myFile.write("\n==================================\n")
        myFile.write("Date: " + str(time.strftime("%m/%d/%Y")) + " -- Time: " + str(time.strftime("%I:%M:%S")))
        myFile.write("\n==================================\n")
        myFile.close()

My Program

from tkinter import *
import time
import clcClass

root = Tk()
root.title('skClc v1')

clc = clcClass.CalcFunc()

clc.logLbl()

clc.getBtn = StringVar()

ent = Entry(root, textvariable=clc.getBtn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
ent.grid(row=0, columnspan=3, pady=10)

button1 = Button(root, text="1", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('1'))
button2 = Button(root, text="2", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('2'))
button3 = Button(root, text="3", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('3'))
button4 = Button(root, text="4", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('4'))
button5 = Button(root, text="5", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('5'))
button6 = Button(root, text="6", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('6'))
button7 = Button(root, text="7", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('7'))
button8 = Button(root, text="8", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('8'))
button9 = Button(root, text="9", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('9'))
button0 = Button(root, text="0", height=1, width=5, bg='light blue', command=lambda:onScreen('0'))
buttonP = Button(root, text="+", height=1, width=5, bg='gray', command=lambda:clc.onScreen('+'))
buttonM = Button(root, text="-", height=1, width=5, bg='gray', command=lambda:clc.onScreen('-'))
buttonMM = Button(root, text="x", height=1, width=5, bg='gray', command=lambda:clc.onScreen('*'))
buttonDD = Button(root, text="รท", height=1, width=5, bg='gray', command=lambda:clc.onScreen('/'))
buttonEE = Button(root, text="=", height=1, width=5, bg='light green', command=clc.evaL)
buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)

button1.grid(row=1, column=0, pady=5)
button2.grid(row=1, column=1, pady=5)
button3.grid(row=1, column=2, pady=5)
button4.grid(row=2, column=0, pady=5)
button5.grid(row=2, column=1, pady=5)
button6.grid(row=2, column=2, pady=5)
button7.grid(row=3, column=0, pady=5)
button8.grid(row=3, column=1, pady=5)
button9.grid(row=3, column=2, pady=5)
button0.grid(row=4, column=0, pady=5)
buttonP.grid(row=4, column=1, pady=5)
buttonM.grid(row=4, column=2, pady=5)
buttonEE.grid(row=5, column=0, pady=5)
buttonDD.grid(row=5, column=1, pady=5)
buttonMM.grid(row=5, column=2, pady=5)
buttonCC.grid(row=6, column=0, pady=5, columnspan=3)

root.maxsize(140,245);
root.minsize(140,245);

root.mainloop()
1
Are you importing your file? If yes, what is the error you are getting? - Lafexlos
<code>Exception in Tkinter callback Traceback (most recent call last): File "C:\Python34\lib\tkinter_init_.py", line 1533, in call return self.func(*args) File "C:\Users\SK\Desktop\PROJECT\clcClass.py", line 17, in clearScreen ent.delete(0, END) NameError: name 'ent' is not defined<code> - user5557886
Since clearScreen and ent are in different files, they have no idea if they exist. You need to import them to make sure they know each other. - Lafexlos
@Lafexlos I updated my entire code - user5557886

1 Answers

1
votes
ent = Entry(root, ....)
clc = clcClass.CalcFunc(ent)

class CalcFunc:
   def __init__(self, entry):
       self.entry = entry

   def clearScreen(self):
      self.log("CLEAR (CE)")
      self.entry.delete(0, END)

Here's an abbreviated example:

#my_entry.py

from tkinter import END
import time

class EntryWithLogger:

    def __init__(self, entry):
        self.entry = entry

    def log(self, val):
        with open("log.dat", "a") as my_file: #Automatically closes the file--even if an exception occurs, which is not the case with my_file.close().
            my_file.write("%s\n" % val)

    def onScreen(self, i_val):
        self.log(i_val)
        self.entry.insert(END, i_val)    

    def clearScreen(self):
        self.log("CLEAR (CE)")
        self.entry.delete(0, END)

Note that I didn't use a StringVar(), which doesn't appear to be necessary. If you need it, you can always pass it as an argument to __init__(), then store it on self.


import my_entry as me
import tkinter as tk

root = tk.Tk()
root.title("Calculator")
root.geometry("+100+50") #("300x500+200+10") dimension, position

entry = tk.Entry(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
entry.grid(row=0, columnspan=3, pady=10)
entry_with_logger = me.EntryWithLogger(entry)

#Create the buttons in a loop:
for i in range(10):
    row_num, col_num = divmod(i, 3) #divmod(7, 2) => (3, 1), divmod(0, 3) => (0, 0), divmod(4, 3) => (1, 1)
    row_num += 1
    button_text = str(i)

    tk.Button(root, text=button_text, 
                    height=1, 
                    width=5, 
                    bg='light blue', 
                    command=lambda x=button_text: entry_with_logger.onScreen(x)
    ).grid(row=row_num, column=col_num, pady=5)

#Put the clear button at the bottom of the grid:
tk.Button(root, text="CLEAR (CE)", 
             height=1, 
             width=20, 
             bg='orange', 
             command=entry_with_logger.clearScreen
).grid(row=row_num+1, columnspan=3) #columnspan tells grid() to use 3 cells for the button,
                                    #and the button will be centered by default.

root.mainloop()

Or, you could do it like this:

#my_entry.py

from tkinter import Entry, END
import time

class EntryWithLogger(Entry):

    #Because __init__() is not implemented, the parent class's __init__() gets  
    #called, so you create an EntryWithLogger just like you would an Entry.

    def log(self, val):
        with open("log.dat", "a") as my_file: #Automatically closes the file--even if there is an exception, which is not the case with my_file.close().
            my_file.write("%s\n" % val)

    def onScreen(self, i_val):
        self.log(i_val)
        self.insert(END, i_val)    

    def clearScreen(self):
        self.log("CLEAR (CE)")
        self.delete(0, END)

import my_entry as me
import tkinter as tk

root = tk.Tk()
root.title("Calculator")
root.geometry("+100+50") #("300x500+200+10") dimension, position

entry = me.EntryWithLogger(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
entry.grid(row=0, columnspan=3, pady=10)

#Create the buttons in a loop:
for i in range(10):
    row_num, col_num = divmod(i, 3) #divmod(7, 2) => (3, 1), divmod(0, 3) => (0, 0), divmod(4, 3) => (1, 1)
    row_num += 1
    button_text = str(i)

    tk.Button(root, text=button_text, 
                    height=1, 
                    width=5, 
                    bg='LightBlue', 
                    command=lambda x=button_text: entry.onScreen(x)
    ).grid(row=row_num, column=col_num, pady=5)

#Put the clear button at the bottom of the grid:
tk.Button(root, text="CLEAR (CE)", 
             height=1, 
             width=20, 
             bg='orange', 
             command=entry.clearScreen
).grid(row=row_num+1, columnspan=3) #columnspan tells grid() to use 3 cells for the button,
                                    #and the button will be centered by default.

root.mainloop()