Python tkinter,从类中清除Entry小部件

时间:2015-11-13 08:04:54

标签: python class tkinter tkinter-entry

这是我正在调用的类和来自不同文件的函数

class CalcFunc:

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

这是条目框

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

这是我点击清除条目框的按钮

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

我不确定语法是什么,基本上可以从类中清除Entry小部件。当我在同一个文件中使用它时,该代码有效,但我的项目要求它在一个单独的文件中。它是计算器的类项目,“清除”按钮清除Entry小部件。如果有帮助,我可以发布我的整个代码。谢谢。

---- ---- EDIT

我的班级

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()

我的计划

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 个答案:

答案 0 :(得分:1)

da.InsertCommand.Parameters.Add("@NoAMiddleName", SqlDbType.VarChar).Value = txtMname.Text;
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)

请注意,我没有使用StringVar(),这似乎不是必需的。如果您需要,可以随时将其作为参数传递给#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) ,然后将其存储在__init__()上。

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()
#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)