鱼C论坛

 找回密码
 立即注册
查看: 2410|回复: 3

[已解决]Tkinter中如何实现动态添加Entry,同时可知哪个Entry被修改

[复制链接]
发表于 2020-8-2 22:13:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
第一次发帖,不太知道规矩,自学小马甲和其他人的课程半年,目前正在用Tkinter编写一个小程序,遇到一个问题,我尽量说明的非常清楚,希望可以尽快得到大神指点。

WeChat 圖片_20200802220337.png

这是一个输入多个信息然后得到计算结果的程序,通过加减按钮可以实现增加和删除行。
添加Entry实际我已经实现了,代码如下:
  1.             def NewMetalPart():

  2.                 global num, rownum, ConfirmButton, v1, v2, v3, v4, v5, a, b, c, d, e, AddButton, BasisPrice, Weight, Utilization, Manufacturing, Profit, Update, Test
  3.                 rownum += 1

  4.                 v1 = StringVar()    #5个可变字符串,对应下面基价,重量,利用率,费率,三项费用
  5.                 v2 = StringVar()
  6.                 v3 = StringVar()
  7.                 v4 = StringVar()
  8.                 v5 = StringVar()

  9.                 NumberText = Label(self.Frame2, text=num)   #最左边的序号框
  10.                 NumberText.grid(row=rownum)

  11.                 BasisPriceText = Label(self.Frame2, text='原材料基价', width=8, height=1)        #基价框
  12.                 BasisPriceText.grid(row=rownum, column=1)
  13.                 BasisPrice = Entry(self.Frame2, textvariable=v1, width=8, validate="focusout", validatecommand=BasisPriceTest, invalidcommand=BasisPriceUpdate)
  14.                 BasisPrice.widgetName = str(rownum)
  15.                 BasisPrice.grid(row=rownum, column=2)


  16.                 WeightText = Label(self.Frame2, text='零件重量', width=8, height=1)     #重量框
  17.                 WeightText.grid(row=rownum, column=3)
  18.                 Weight = Entry(self.Frame2, textvariable=v2, width=8, validate="focusout", validatecommand=WeightTest, invalidcommand=WeightUpdate)
  19.                 Weight.widgetName = str(rownum)
  20.                 Weight.grid(row=rownum, column=4)

  21.                 UtilizationText = Label(self.Frame2, text='材料利用率', width=8, height=1)       #利用率框
  22.                 UtilizationText.grid(row=rownum, column=5)
  23.                 Utilization = Entry(self.Frame2, textvariable=v3, width=8, validate="focusout", validatecommand=UtilizationTest, invalidcommand=UtilizationUpdate)
  24.                 Utilization.grid(row=rownum, column=6)

  25.                 ManufacturingText = Label(self.Frame2, text='制造费率', width=8, height=1)      #制造费率框
  26.                 ManufacturingText.grid(row=rownum, column=7)
  27.                 Manufacturing = Entry(self.Frame2, textvariable=v4, width=8, validate="focusout", validatecommand=ManufacturingTest, invalidcommand=ManufacturingUpdate)
  28.                 Manufacturing.widgetName = str(rownum)
  29.                 Manufacturing.grid(row=rownum, column=8)

  30.                 ProfitText = Label(self.Frame2, text='三项费用', width=8, height=1)     #三项费用框
  31.                 ProfitText.grid(row=rownum, column=9)
  32.                 Profit = Entry(self.Frame2, textvariable=v5, width=8, validate="focusout", validatecommand=ProfitTest, invalidcommand=ProfitUpdate)
  33.                 Profit.widgetName = str(rownum)
  34.                 Profit.grid(row=rownum, column=10)

  35.                 AddButton = Button(self.Frame2, text='+', width=1, command=AddPart)     #加行按钮,绑定了AddPart方法
  36.                 AddButton.grid(row=rownum, column=13)

  37.                 DelButton = Button(self.Frame2, text='-', width=1, command=DelPart)     #删除按钮,绑定了DelPart方法
  38.                 DelButton.widgetName = str(rownum)
  39.                 DelButton.grid(row=rownum, column=12)
  40.                 num += 1

  41.             NewMetalPart()
  42.             #相当于在最顶上选择这是金属零件,就进入上面的NewMetalPart方法里面执行一遍

  43.             ButtonRowNum = rownum + 1                                                                   #最下面的确定按钮,绑定了Total方法
  44.             ConfirmButton = Button(self.Frame2, text='确定', width=8, height=1, command=Total)
  45.             ConfirmButton.grid(row=ButtonRowNum, columnspan=12)
复制代码


增加行的方法 AddPart的代码如下:
  1.             def AddPart():
  2.                 global ButtonRowNum, ConfirmButton, AddButton

  3.                 Calculate()

  4.                 AddButton.destroy()
  5.                 DelButton = Button(self.Frame2, text='-', width=1, command=DelPart)
  6.                 DelButton.grid(row=rownum, column=12)

  7.                 ButtonRowNum += 1
  8.                 ConfirmButton.destroy()
  9.                 ConfirmButton = Button(self.Frame2, text='确定', width=8, height=1, command=Total)
  10.                 ConfirmButton.grid(row=ButtonRowNum, columnspan=12)

  11.                 NewMetalPart()
复制代码


实际就是把最初的代码又运行一遍,添加一行新的。

如果正常运行,每一行的5个数据我都保存到对应的字典里面,用行数命名key,然后计算各自每个零件的价格和总体的价格。

我目前的问题是,假如现在我已经有了3行数据,我要更新第2行的基价,因为基价的Entry名字都叫BasisPrice,故更新后的Entry里面的数据对应不到第2个零件上。

而且由于添加的行数也是未知的,我没办法在命名上进行区分。

这个问题已经困扰我好多天了,借此想问一下各位大神有何高招,我觉得是我的添加新行的方法有问题,导致这个问题无解,如果有添加新行的方法的话也还请赐教。

十分感谢!
最佳答案
2020-8-2 22:15:27


你可以将添加完的组件加入到一个列表中去,然后通过列表下标索引来索引出对于组件即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-2 22:15:27 | 显示全部楼层    本楼为最佳答案   


你可以将添加完的组件加入到一个列表中去,然后通过列表下标索引来索引出对于组件即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-11-21 08:54:36 | 显示全部楼层
我也遇到了类似的问题,请问问题解决了吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 22:44:21 | 显示全部楼层
本帖最后由 xux212 于 2021-12-17 22:57 编辑

当初上网找代码找的好苦,现在会一点点了,贴上献丑。
////////////////////////////////////////////////////////////////////////////
import tkinter as tk
import tkinter.messagebox as msg
import ctypes

class pub():
    def str_winapi_contel(api): #返回适当位置分辨率
        win_api = ctypes.windll.user32
        if api == 'x':
            return str(int((int(win_api.GetSystemMetrics(0))-618)/2-100))
        if api == 'y':
            return str(int((int(win_api.GetSystemMetrics(1))-312)/2-100))
    def str_winapi(api): #返回全屏分辨率
        win_api = ctypes.windll.user32
        if api == 'x':
            return str(win_api.GetSystemMetrics(0))
        if api == 'y':
            return str(win_api.GetSystemMetrics(1))

class log(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("element")
        self.geometry("618x382" + "+" + pub.str_winapi_contel("x") + "+" + pub.str_winapi_contel("y"))
        self.frame = tk.Frame(self)
        self.init_widget()

    def init_widget(self):
        self.count = 1
        self.list_element_names = locals()
        self.list_numbers = locals()
        self.label_units = locals()
        self.temp_element = []
        self.label_name = tk.Label(self.frame, text='姓名')
        self.label_name.grid(row=1, column=1)
        self.list_name = tk.Entry(self.frame)
        self.list_name.grid(row=2, column=1)
        self.label_tel = tk.Label(self.frame, text='电话')
        self.label_tel.grid(row=3, column=1)
        self.list_tel = tk.Entry(self.frame)
        self.list_tel.grid(row=4, column=1, )
        self.label_element_name = tk.Label(self.frame, text='名称')
        self.label_element_name.grid(row=1, column=2)
        self.label_number = tk.Label(self.frame, text='数量')
        self.label_number.grid(row=1, column=3)
        self.add()
        self.btn_puls = tk.Button(self.frame, text="+", width=3, command=self.add)
        self.btn_puls.grid(row=2, column=5)
        self.btn_back = tk.Button(self.frame, text="保存", width=10, command=lambda: self.destroy())
        self.btn_back.grid(row=16, column=1)
        self.btn_back = tk.Button(self.frame, text="返回", width=10, command=lambda: self.destroy())
        self.btn_back.grid(row=16, column=2, pady=2)
        self.frame.grid(padx=50, pady=5, sticky=tk.N + tk.E + tk.W)

    def add(self):
        if self.count < 10:
            self.list_element_names[str(self.count)] = tk.Entry(self.frame, width=30)
            self.list_element_names[str(self.count)].grid(row=self.count + 1, column=2,padx=10)
            self.list_numbers[str(self.count)] = tk.Entry(self.frame, width=5)
            self.list_numbers[str(self.count)].grid(row=self.count + 1, column=3, padx=10)
            self.label_units[str(self.count)] = tk.Label(self.frame, text='个')
            self.label_units[str(self.count)].grid(row=self.count + 1, column=4)
            self.count += 1
        else:
            msg.showinfo("self.count", "数量过多")
            return

if __name__ == "__main__":
    app = log()
    app.mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 07:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表