4

I am trying to create a simple interface to access the name array with first, last, previous and next functionality. But the global variable I am using as a position tracker is not working. I have already referred to various question. Would really appreciate the help. Here is the code.

from tkinter import Tk, Label, Entry, Button, StringVar, IntVar

window = Tk() 

name_array = [('a1','a2','a3'), ('b1','b2','b3'), ('c1','c2','c3'),('d1','d2','d3')]
global position_track
position_track = IntVar()

first_name = StringVar()
last_name = StringVar()
email = StringVar()

def return_value(pos):
    first_name.set(name_array[pos][0])
    last_name.set(name_array[pos][1])
    email.set(name_array[pos][2])

def update_value(pos):
    name_array[pos] = (first_name.get(), last_name.get(), email.get())

def first_value():
    global position_track
    return_value(0)
    postion_track.set(0)

def last_value():
    global position_track
    return_value(-1)
    postion_track.set(-1)

def next_value():
    global position_track
    if position_track.get() == len(name_array):
        position_track.set(1)
    temp = postion_track.get()
    return_value(temp + 1)
    postion_track.set(temp + 1)

def prev_value():
    global position_track
    if position_track.get() == -1:
        position_track.set(len(name_array - 1))
    temp = postion_track.get()
    return_value(temp - 1)
    postion_track.set(temp - 1)

label_first_name = Label(window, text = 'First Name:', justify = 'right', padx = 5) 
entry_first_name = Entry(window, textvariable = first_name) 
label_last_name = Label(window, text = 'Last Name:', justify = 'right', padx = 5) 
entry_last_name = Entry(window, textvariable = last_name) 
label_email = Label(window, text = 'Email Address:', justify = 'right', padx = 5) 
entry_email = Entry(window, textvariable = email) 


button_first = Button(window, text = 'First', command = first_value) 
button_last = Button(window, text = 'Last', command = last_value) 
button_prev = Button(window, text = 'Prev', command = prev_value) 
button_next = Button(window, text = 'Next', command = next_value)
button_quit = Button(window, text = 'Quit') 
button_quit.configure(command=window.destroy)

labels = [label_first_name, label_last_name, label_email]
entries = [entry_first_name, entry_last_name, entry_email]
buttons = [button_first, button_last, button_prev, button_next, button_last, button_quit]


for i in range(3):
    labels[i].grid(row = i, column = 0, sticky = 'W')
    entries[i].grid(row = i, column = 1, columnspan = 6)

for j in range(6):
    buttons[j].grid(row = 3, column = j, sticky = 'E')




window.mainloop()
Strommer
  • 313
  • 1
  • 2
  • 10

1 Answers1

6

Too many typos. Plus, you don't need to declare a global in the outermost program space, just in the function defs. Corrected working code ->

from tkinter import Tk, Label, Entry, Button, StringVar, IntVar

window = Tk()

name_array = [('a1','a2','a3'), ('b1','b2','b3'), ('c1','c2','c3'),('d1','d2','d3')]
position_track = IntVar()

first_name = StringVar()
last_name = StringVar()
email = StringVar()

def return_value(pos):
    first_name.set(name_array[pos][0])
    last_name.set(name_array[pos][1])
    email.set(name_array[pos][2])

def update_value(pos):
    name_array[pos] = (first_name.get(), last_name.get(), email.get())

def first_value():
    global position_track
    return_value(0)
    position_track.set(0)

def last_value():
    global position_track
    return_value(-1)
    position_track.set(-1)

def next_value():
    global position_track
    if position_track.get() == len(name_array):
        position_track.set(1)
    temp = position_track.get()
    return_value(temp + 1)
    position_track.set(temp + 1)

def prev_value():
    global position_track
    if position_track.get() == -1:
        position_track.set(len(name_array) - 1)
    temp = position_track.get()
    return_value(temp - 1)
    position_track.set(temp - 1)

label_first_name = Label(window, text = 'First Name:', justify = 'right', padx = 5)
entry_first_name = Entry(window, textvariable = first_name)
label_last_name = Label(window, text = 'Last Name:', justify = 'right', padx = 5)
entry_last_name = Entry(window, textvariable = last_name)
label_email = Label(window, text = 'Email Address:', justify = 'right', padx = 5)
entry_email = Entry(window, textvariable = email)


button_first = Button(window, text = 'First', command = first_value)
button_last = Button(window, text = 'Last', command = last_value)
button_prev = Button(window, text = 'Prev', command = prev_value)
button_next = Button(window, text = 'Next', command = next_value)
button_quit = Button(window, text = 'Quit')
button_quit.configure(command=window.destroy)

labels = [label_first_name, label_last_name, label_email]
entries = [entry_first_name, entry_last_name, entry_email]
buttons = [button_first, button_last, button_prev, button_next, button_last, button_quit]


for i in range(3):
    labels[i].grid(row = i, column = 0, sticky = 'W')
    entries[i].grid(row = i, column = 1, columnspan = 6)

for j in range(6):
    buttons[j].grid(row = 3, column = j, sticky = 'E')

window.mainloop()
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
  • Thanks the code works fine. But it still spits out errors when I click on prev, or next – Strommer Nov 14 '13 at 15:11
  • 1
    It was again a typo in `pre_value`. `position_track.set(len(name_array - 1))` should be `position_track.set(len(name_array) - 1)` – shad0w_wa1k3r Nov 14 '13 at 15:17
  • 1
    Corrected another typo (hopefully the last) Please use the latest code. – shad0w_wa1k3r Nov 14 '13 at 15:21
  • If it's not too much trouble can you also tell me what is the problem with the update option, which I have defined? – Strommer Nov 14 '13 at 15:29
  • 1
    Well not at all. Your `update` option is not being used! I don't see any other issues with it. You might need another button for that task. Also, if you are going to update the `name_array` you must not make it a list of tuples, but, a list of lists. – shad0w_wa1k3r Nov 14 '13 at 15:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41199/discussion-between-strommer-and-ashish-nitin-patil) – Strommer Nov 14 '13 at 15:34