beginner
beginner

Reputation: 1

Stop the error when trying to manipulate the items of a Listbox, but no item is selected?

I am a beginner in tkinter. I am making a list of names. You can delete, select and edit it, but if I don't select anything in the list and click these buttons, it says:

Exception in Tkinter callback Traceback (most recent call last): File
"C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__ return
self.func(*args) File "C:\Users\user\Desktop\HOW_TOUGH - NEW\Change_user.py",
line 60, in Edit (idx, ) = d ValueError: need more than 0 values to unpack'''

I am planning to disable the buttons if the user doesn't click anything but I am not expert enough. Here's my code (it's a child window)

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class Nick:

    def __init__(self, master ):
        self.master = master
        self.window = Toplevel(master)
        self.window.title('Change User')
        self.window.geometry('300x300')
        self.window.minsize(300, 300)
        self.window.maxsize(300, 300)

        self.nickname = StringVar()

        self.lb = Listbox(self.window, selectmode = 'SINGLE')

        f= open('users.txt','r')
        rec = f.readlines()
        f.close()

        for i in rec:
            p = i.find('|')
            nickname = i[:p]
            self.lb.insert(END, nickname)

        self.lb.pack()

        self.Ed = ttk.Button(self.window, text = 'Edit', command = self.Edit).pack()
        self.Del = ttk.Button(self.window, text = 'Delete', command = self.Delete).pack()
        self.Bac = ttk.Button(self.window, text = 'Back', command = self.Back).pack()
        self.Okay = ttk.Button(self.window, text = 'Ok', command = self.Ok).pack()

    def Back(self):
        self.window.destroy()

    def Delete(self):

        d = self.lb.curselection()
        (idx, ) = d
        self.lb.delete(idx)

        f = open('users.txt','r')
        r = f.readlines()
        f.close()

        rec = r[idx]
        r.remove(rec)

        f = open('users.txt','w')
        new = ''.join(r)
        r = f.write(new)
        f.close()

        messagebox.showinfo(title='Success', message = 'Delete successful')

    def Edit(self):
        d = self.lb.curselection()
        (idx, ) = d
        import Edit as Edet
        Edet.Edit(self.master, idx)

    def Ok(self):

        d = self.lb.curselection()
        (idx, ) = d
        get = self.lb.get(idx)
        self.window.destroy()
        print (get)
        print (d)

Upvotes: 0

Views: 56

Answers (1)

TidB
TidB

Reputation: 1759

The method curselection() returns an empty tuple when nothing is selected. You can skip those methods just by adding a

if not d:
    return

If you want to gray out your buttons, you can do this:

button["state"] = DISABLED

Note that this won't work currently with your code as you did this:

self.button = ttk.Button(...).pack()

The problem lies in the call of pack() which returns None, effectively binding self.button to None. Just assign the button object to the variable first and then pack it. Furthermore, it's not recommended to import * from Tkinter because you're dropping ~190 names in your namespace. Just use

import tkinter as tk

Upvotes: 2

Related Questions