python - 使用 Tkinter 我正在尝试添加用户输入的值并将其添加到文件中存在的值中,但它不起作用
问题描述
我正在尝试为我的小型企业创建一个非常简单的忠诚度计划。这是我在 Python 或 Tkinter 中的第一个项目。
下面的#POINT ENTRY 部分允许用户输入适当数量的点数。然后它使用“addpoints”从文本文件(扩展名为“.cust”)中提取当前点,并添加用户输入的内容并将其写回文本文件。
不幸的是,它实际上只是用“.!Entry26”替换了整行
任何指导将不胜感激。
#POINT ENTRY
pointlabel = Label(root, text="Enter Earned/Spent Points")
pointlabel.pack()
pointlabel.place(x = 46, y = 95)
pointenter = Entry(root, bg="#E9E9E9")
pointenter.config(font="bold")
pointenter.pack()
pointenter.place(x = 50, y = 120, height="30", width="140")
addbutton = Button(root, bg="green", fg="white", text=" + ", command=addpoints)
addbutton.config(font='bold')
addbutton.pack()
addbutton.place(x = 201, y = 118)
subbutton = Button(root, bg="red", text="-")
subbutton.config(font='bold')
subbutton.pack()
subbutton.place(x = 251, y = 118)
def addpoints():
file = open("assets\\" + IDentry.get() + ".cust", 'r+')
currpts = file.read(0)
updatepoints = sum(currpts, pointenter)
file.write(str(updatepoints))
file.close()
解决方案
这是一个醒目的例子:
尝试:
from tkinter import *
class App:
def __init__(self, root):
self.pointenter = Entry(root,font=(None, 14), width=50)
self.pointenter.pack()
addbutton = Button(root, bg="green", fg="white", text=" + ", command=self.addpoints)
addbutton.config(font='bold')
addbutton.pack()
def addpoints(self):
self.currpts = '50' # a string
print(float(self.currpts)+ float(self.pointenter.get()))
if __name__=='__main__':
root = Tk()
app = App(root)
root.mainloop()
我刚开始时发现非常有用的一件事是对所有变量执行 print()。希望这可以帮助。
推荐阅读
- spring-boot - 从 Spring Boot 写入 2 个数据库的问题
- python-3.x - 如何通过考虑节点名称来测量图形编辑距离
- sql - 列需要根据值进行分隔并对其进行分组
- python - 使用 Tkinter 创建 GUI 以在 pandas 代码中选择 CSV 文件
- javascript - 为什么我的图层控件中没有激活任何选项?
- python - 通过 kwargs 添加功能选项(例如,详细)
- matlab - 在没有 for 循环的情况下更改条形图中的单个条形颜色
- java - 显示整数数组排列的 Java 程序
- javascript - 如何使用 Yarn Workspaces 在 Detox 测试中使用 ES6 导入?
- javascript - 动画比例旋转 svg 元素