简体   繁体   English

Python Tkinter 用于循环创建和更新的输入框

[英]Python Tkinter Entry Box For Loop creation and Updation

I have created a tkinter GUI using a for loop.我使用 for 循环创建了一个 tkinter GUI。 it has some labels, entry boxes and 2 buttons.它有一些标签、输入框和 2 个按钮。 One buttons pull whatever has been entered in the entry boxes and second button populate the entry boxes based on a list.一个按钮拉出输入框中输入的任何内容,第二个按钮根据列表填充输入框。 First button is working fine, but second button (FETCH button) gives this error " Entry' object has no attribute 'set' ".第一个按钮工作正常,但第二个按钮(FETCH 按钮)给出此错误“ Entry' object has no attribute 'set' ”。 Can someone help and confirm what am I doing wrong?有人可以帮助确认我做错了什么吗?

below is the code下面是代码

from tkinter import *

bg_color_2 = "#d9ffff"
background_color = bg_color_2 
button_color = "#ffe08a"
white_font_color = "##ffffff"
black_font_color = "000000"
relief_type = "ridge"

width_of_window = 938
height_of_window = 833

root = Tk()
root.configure(bg= background_color)

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

x_coordinate = (screen_width/2) - (width_of_window/2)
y_coordinate = (screen_height/2) - (height_of_window/2)

root.geometry("%dx%d+%d+%d" % (width_of_window, height_of_window, x_coordinate, y_coordinate))

def send():
    rule_entry = []
    for entries in my_entries:
        rule_entry.append(entries.get())

def fetch():
    rule_label_list = ['test1', 'test2', 'test3', 'test4', 'test5', 'test5', 'test6', 'test7', 'test8', 'test9', 'test10', 'test11', 'test12', 'test13', 'test14', 'test15', 'test16', 'test17', 'test18']
    i = 0
    print(my_entries)
    for a in my_entries:
        a.set(rule_label_list[i])
        i + i + 1

root.title("Result Rule")
root.minsize(width_of_window, height_of_window)
root.maxsize(width_of_window, height_of_window)

rule_label_list = ['label1', 'label2', 'label3', 'label4', 'label5', 'label5', 'label6', 'label7', 'label8', 'label9', 'label10', 'label11', 'label12', 'label13', 'label14', 'label15', 'label16', 'label17', 'label18']
my_entries = []
for a in range(0,19):
    my_label = Label(root, text=rule_label_list[a], fg= 'black', bg= bg_color_2, justify = LEFT, width=15,font=("bold", 10), anchor=W, borderwidth = 2)
    my_label.grid(row=a, column=0, pady=5, padx=5)

    my_entry_box = Entry(root, fg= 'black', bg='#FFFFFF', justify = LEFT, width=65,font=("bold", 10))
    my_entry_box.grid(row=a, column=1, columnspan = 4, pady=5)

    my_entries.append(my_entry_box)

Button(root, text='SEND', bg='#ffc200',fg='black', font=("bold", 10), width=15, height = 1,command = send).grid(row=20,column=0,pady=5, padx=5)
Button(root, text='FETCH', bg='#ffc200',fg='black', font=("bold", 10), width=15, height = 1,command = fetch).grid(row=20,column=1,pady=5, padx=5)

root.mainloop()
a = DoubleVar()
a.set(rule_label_list[i])

Try to add DoubleVar in line 34. It helped me尝试在第 34 行添加 DoubleVar。它帮助了我

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

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