简体   繁体   中英

Python Tkinter Entry Box For Loop creation and Updation

I have created a tkinter GUI using a for loop. it has some labels, entry boxes and 2 buttons. 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' ". 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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