繁体   English   中英

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

[英]Python tkinter, clearing an Entry widget from a class

这是我正在调用的类,并且是来自其他文件的函数

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小部件。 如果有帮助,我可以发布整个代码。 谢谢。

- - 编辑 - -

我的课

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

这是一个简短的示例:

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

请注意,我没有使用StringVar(),这似乎不是必需的。 如果需要,可以始终将其作为参数传递给__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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM