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

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

python entry如何清空_Python tkinter,从类中清除Entry小部件相关推荐

  1. python怎么存储数据_Python:如何在类中存储数据并继承

    我对课程很陌生,我已经写了一个班来做熊猫的操作. @分类方法 ,但我不知道如何使用它们. 我的班级目标: 我想写一个权限类.它应该加载/写入一个json文件,并且应该能够添加更多的权限. 编辑: 读了 ...

  2. python tkinter控件_Python——Tkinter窗口的函数,Pythontkinter,视窗,功能,部件

    tkinter简介 Tkinter 是使用 python 进行窗口视窗设计的模块.Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.作为 pytho ...

  3. python设置窗口焦点_Python Tkinter PanedWindow窗口布局管理

    PanedWindow小部件的作用类似于Container小部件,其中包含一个或多个水平或垂直排列的子窗口小部件(窗格).通过使用鼠标移动称为框格的分隔线,用户可以调整子窗格的大小. 每个窗格仅包含一 ...

  4. python中的替换函数_python:替换模块类中的函数

    我试图替换类中定义的函数,以便在不更改实际代码的情况下修改其函数(如内部工作). 我以前从来没有这样做过,因此在更换它时遇到一些问题. 更改代码会让我访问python库中的包,这不是一个很好的选择. ...

  5. python 常数怎么表达_Python的常数类

    我在看这个问题的答案:是否可以在枚举内定义类常量? 最让我感兴趣的是伊桑·弗曼的回答中不断出现的问题. class Constant: def __init__(self, value): self. ...

  6. python类内部方法调用_python如何调用类中的方法

    调用同一个类中的方法 首先类中的方法在定义的时候需要先加参数self,例如:def SaveData(self,ip): print(ip)如果无self参数则不能在同一个类中调用(之前一直在这里犯错 ...

  7. python单选按钮控件_Python Tkinter Radiobutton单选按钮

    Python Tkinter Radiobutton单选按钮 Radiobutton小部件用于在python应用程序中实现一个多选项.它向用户显示多个选项,用户只能从中选择一个.我们可以将不同的方法与 ...

  8. python设计棋牌游戏_python开发棋牌类游戏

    pycharm专门针对用户打造的一种可以进行编辑的工具,它的功能设置比较强大,而且具有跨平台的使用特性,能方便用户通过跨平台的方式使用该软件,有效节省的使用时间,那么这款详情>> 阅读: ...

  9. python程序内存分析_python 如何测量运行中的程序内存 -- Valgrind

    介绍 通常我们可以用python profiler去分析应用程序中哪个模块被多次调用和那个程序部分运行的速度较为缓慢,但是并不能够准确给出我们应用程序在运行中在内存中占用的大小. 比如说在金融数据中会 ...

最新文章

  1. 基于聚类的图像分割-Python
  2. RAD Studio (Delphi) Firemonkey 教程
  3. 使用OpenCV画折线图
  4. mysql存储过程中as_mysql - 存储过程mySQL语法错误意外“ AS” - 堆栈内存溢出
  5. 今晚直播丨抢鲜体验-openGauss入门
  6. 使用npm安装一些包失败了的看过来(npm国内镜像介绍)
  7. ping的时候怎么暂停_dos命令pause教程,?暂停bat批处理脚本程序,?请按任意键继续...
  8. 局部变量AND全局变量
  9. paip.复制文件 文件操作 api的设计uapi java python php 最佳实践
  10. matlab数值型数据默认为,matlab默认数据类型
  11. Js获取时间-天干地支空亡
  12. php 解决Chrome Cookie 的 SameSite 属性导致无法写入cookie问题
  13. 新浪微博开发平台接入流程(1)---注册应用
  14. 安卓手机软件性能测试,四款安卓公交查询软件基本性能测试
  15. 坑!U+03bf和U+006f
  16. FPGA串口接收学习
  17. python读取短信验证码_我用Python给你发了个短信验证码,你也来试试
  18. ueditor编辑器抓取页面背景图片background-image或background
  19. 计算机四级证书有哪些用处
  20. Ubuntu安装软件是始终出现dpkg错误(转载,原文链接:https://blog.csdn.net/cyf15238622067/article/details/53534629)

热门文章

  1. 计算机专业英语词组,计算机专业英语词组.doc
  2. android 中 虚拟键盘
  3. CPT、CPM、CPC、CPA、CPS
  4. 人工智能是什么?人工智能行业前景如何
  5. Pygame-飞机大战
  6. omnet 收发信息
  7. 超强跑得快机器人智能算法深度研究与设计
  8. Python源码学习(一)
  9. gb酱油和gbt酱油哪个好_酱油那个gb18186是纯酿造的吗
  10. kubelet重新安装新版本报错Unit kubelet.service entered failed state.