pythonentry如何清空_Pythontkinter,从类中清除Entry⼩部件这是我正在调⽤的类,并且是来⾃其他⽂件的函数
class CalcFunc:
def clearScreen(lf):
lf.log("CLEAR (CE)")
ent.delete(0, END)
人民英雄永垂不朽这是输⼊框
ent = Entry(root, Btn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
这是我单击以清除输⼊框的按钮
buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)
我不确定基本从类中清除Entry⼩部件的语法是什么。当我将其保存在同⼀⽂件中时,该代码有效,但是我的项⽬要求将其保存在单独的⽂件中。这是⼀个计算器的类项⽬,“清除”按钮清除了Entry⼩部件。如果有帮助,我可以发布我的整个代码。谢谢。海的女儿动画片
- - 编辑 - -
我的课
import time
class CalcFunc:
列的组词
def log(lf, val):
myFile = open(r".\log.dat", "a")
myFile.write("%s\n" % val)
myFile.clo()
def onScreen(lf, iVal):
lf.log(iVal)
currentTxt = ()
updateEnt = lf.getBtn.t(currentTxt + iVal)
描写田园风光的古诗def clearScreen(lf):
lf.log("CLEAR (CE)")
ent.delete(0, END)
def evaL(lf):
lf.log("=")
lf.())
def logLbl(lf):
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.clo()
我的程序
from tkinter import *
import time
import clcClass
root = Tk()
root.title('skClc v1')
clc = clcClass.CalcFunc()
clc.logLbl()
ent = Entry(root, Btn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
button1 = Button(root, text="1", height=1, width=5, bg='light blue', command=Screen('1')) button2 = Button(root, text="2", height=1, width=5, bg='light blue', command=Screen('2')) button3 = Button(root, text="3", height=1, width=5, bg='light blue', command=Screen('3')) button4 = Button(root, text="4", height=1, width=5, bg='light blue', command=Screen('4')) button5 = Button(root, text="5", height=1, width=5, bg='light blue', command=Screen('5')) button6 = Button(root, text="6", height=1, width=5, bg='light blue', command=Screen('6')) button7 = Button(root, text="7", height=1, width=5, bg='light blue', command=Screen('7')) button8 = Button(root, text="8", height=1, width=5, bg='light blue', command=Screen('8')) button9 = Button(root, text="9", height=1, width=5, bg='light blue', command=Screen('9')) button0 = Button(root, text="0", height=1, width=5, bg='light blu
e', command=lambda:onScreen('0')) buttonP = Button(root, text="+", height=1, width=5, bg='gray', command=Screen('+')) buttonM = Button(root, text="-", height=1, width=5, bg='gray', command=Screen('-')) buttonMM = Button(root, text="x", height=1, width=5, bg='gray', command=Screen('*')) buttonDD = Button(root, text="÷", height=1, width=5, bg='gray', command=Screen('/')) 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) id(row=1, column=0, pady=5)
英语作文翻译器id(row=3, column=1, pady=5)
root.minsize(140,245);
root.mainloop()
解决⽅案
ent = Entry(root, ....)
clc = clcClass.CalcFunc(ent)
class CalcFunc:
惊惶无措
def __init__(lf, entry):
< = entry
def clearScreen(lf):
lf.log("CLEAR (CE)")
这是⼀个简短的⽰例:
#my_entry.py
from tkinter import END
import time
class EntryWithLogger:
def __init__(lf, entry):
< = entry
def log(lf, val):
with open("log.dat", "a") as my_file: #Automatically clos the file--even if an exception occurs, which is not the ca with my_file.clo().
my_file.write("%s\n" % val)
def onScreen(lf, i_val):
lf.log(i_val)
def clearScreen(lf):
lf.log("CLEAR (CE)")
请注意,我没有使⽤StringVar(),这似乎不是必需的。如果需要,可以始终将其作为参数传递给__init__(),然后将其存储在上lf。
import my_entry as me
import tkinter as tk
root = tk.Tk()
root.title("Calculator")
entry = tk.Entry(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
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_Screen(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 u 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):
#Becau __init__() is not implemented, the parent class's __init__() gets
#called, so you create an EntryWithLogger just like you would an Entry.
def log(lf, val):
with open("log.dat", "a") as my_file: #Automatically clos the file--even if there is an exception, which is not the ca with my_file.clo().
my_file.write("%s\n" % val)
def onScreen(lf, i_val):
lf.log(i_val)
lf.inrt(END, i_val)
def clearScreen(lf):
创卫
lf.log("CLEAR (CE)")
lf.delete(0, END)
import my_entry as me
import tkinter as tk
root = tk.Tk()
root.title("Calculator")
entry = me.EntryWithLogger(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
#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,