1

I have a problem with entry.select_adjust(). The code is like below.

The error is occurred at self.rename_entry_mpf.select_adjust(END). If I delete this line, the program works properly. But I want to highlight the file name when I click the file to rename. That's why I used select_adjust. With "select_adjust", when I click the file, the entry is created and the name of file is displayed and highlighted. Then I write anything, even though I pushed just return button, the error is occured.

I edited my post and almost code is like below.


def mpf_db(self):
    self.lb_frm_mpf = LabelFrame(frame_photo, text="program")
    self.lb_frm_mpf.pack(side="left", fill="both", expand=True)
    self.lb_frm_db = LabelFrame(frame_photo, text="profile")
    self.lb_frm_db.pack(side="right", fill="both", expand=True)
    self.sbarx_lstbx_mpf = Scrollbar(self.lb_frm_mpf, orient="horizontal")
    self.sbarx_lstbx_mpf.pack(side="bottom", fill="x")
    self.sbary_lstbx_mpf = Scrollbar(self.lb_frm_mpf, orient="vertical")
    self.sbary_lstbx_mpf.pack(side="right", fill="y")
    self.sbarx_lstbx_db = Scrollbar(self.lb_frm_db, orient="horizontal")
    self.sbarx_lstbx_db.pack(side="bottom", fill="x")
    self.sbary_lstbx_db = Scrollbar(self.lb_frm_db, orient="vertical")
    self.sbary_lstbx_db.pack(side="right", fill="y")
    self.name_list = os.listdir(r"C:\Bart\My library\Python\Grinding")
    self.lstbx_mpf = Listbox(self.lb_frm_mpf, selectmode="extended", height=14, xscrollcommand=self.sbarx_lstbx_mpf.set, yscrollcommand=self.sbary_lstbx_mpf.set)
    self.lstbx_mpf.pack(fill="both", expand=True)
    self.lstbx_db = Listbox(self.lb_frm_db, selectmode="extended", height=14, xscrollcommand=self.sbarx_lstbx_db.set, yscrollcommand=self.sbary_lstbx_db.set)
    self.lstbx_db.pack(fill="both", expand=True)
    self.sbarx_lstbx_mpf.config(command=self.lstbx_mpf.xview)
    self.sbary_lstbx_mpf.config(command=self.lstbx_mpf.yview)
    self.sbarx_lstbx_db.config(command=self.lstbx_db.xview)
    self.sbary_lstbx_db.config(command=self.lstbx_db.yview)
    self.lstbx_mpf.delete(0, END)
    self.lstbx_db.delete(0, END)
    for file in self.name_list:
        if file.endswith(".mpf"):
            self.lstbx_mpf.insert(END, file)
        elif file.endswith(".db"):
            self.lstbx_db.insert(END, file)
    self.lstbx_mpf.bind("<Button-3>", self.right_click_mpf)
    self.lstbx_db.bind("<Button-3>", self.right_click_db)
    self.lstbx_db.bind("<Double-Button-1>", self.doubl_left_click_db)
    self.root.bind("<Button-1>", self.rename_cancel)
def right_click_mpf(self, event):
    if self.rename_act_mpf == 1:
        self.rename_entry_mpf.destroy()
        self.rename_act_mpf = 0
    if self.rename_act_db == 1:
        self.rename_entry_db.destroy()
        self.rename_act_db = 0
    self.x_place, self.y_place = event.x, event.y
    self.lstbx_mpf.selection_clear(0, END)
    self.lstbx_mpf.selection_set(self.lstbx_mpf.nearest(event.y))
    self.lstbx_mpf.activate(self.lstbx_mpf.nearest(event.y))
    self.menu = Menu(self.root, tearoff=0)
    self.menu.add_command(label="삭제", command=self.delete_mpf)
    self.menu.add_command(label="이름변경", command=self.rename_mpf)
    try:
        self.menu.tk_popup(event.x_root, event.y_root)
    finally:
        self.menu.grab_release()

def right_click_db(self, event):
    if self.rename_act_db == 1:
        self.rename_entry_db.destroy()
        self.rename_act_db = 0
    if self.rename_act_mpf == 1:
        self.rename_entry_mpf.destroy()
        self.rename_act_mpf = 0
    self.x_place, self.y_place = event.x, event.y
    self.lstbx_db.selection_clear(0, END)
    self.lstbx_db.selection_set(self.lstbx_db.nearest(event.y))
    self.lstbx_db.activate(self.lstbx_db.nearest(event.y))
    self.menu = Menu(self.root, tearoff=0)
    self.menu.add_command(label="삭제", command=self.delete_db)
    self.menu.add_command(label="이름변경", command=self.rename_db)
    try:
        self.menu.tk_popup(event.x_root, event.y_root)
    finally:
        self.menu.grab_release()

def rename_mpf(self):
    self.rename_entry_mpf = Entry(self.lb_frm_mpf, bg="yellow")
    self.rename_entry_mpf.place(x=0, y=16 * self.lstbx_mpf.nearest(self.y_place))
    self.rename_entry_mpf.insert(0, self.lstbx_mpf.get(self.lstbx_mpf.curselection()))
    self.rename_entry_mpf.focus()
    self.rename_entry_mpf.select_adjust(END)
    self.rename_entry_mpf.bind("<Return>", self.rename_enter_mpf)
    self.rename_act_mpf = 1

    def rename_enter_mpf(self, event):
    if self.rename_entry_mpf.get().endswith(".mpf"):
        os.rename(self.lstbx_mpf.get(self.lstbx_mpf.curselection()), self.rename_entry_mpf.get())
    else:
        os.rename(self.lstbx_mpf.get(self.lstbx_mpf.curselection()), self.rename_entry_mpf.get() + ".mpf")
    self.rename_entry_mpf.destroy()
    self.rename_act_mpf = 0
    self.lstbx_mpf.delete(0, END)
    self.name_list = os.listdir(r"C:\Bart\My library\Python\Grinding")
    for file in self.name_list:
        if file.endswith(".mpf"):
            self.lstbx_mpf.insert(END, file)

Even I wrote any name but it is entered "". How can I solve this problem?

  • 2
    Please include the full traceback so that we can see what line is causing the error. – Bryan Oakley Oct 15 '21 at 17:48
  • I would hazard a guess from the error message that this is a `Listbox` error not a `Entry` error. This type of error can be generated by attempting to get data from a listbox with no data or from a listbox that has no active selection cursor. If this is the case then populate your listbox with some data and use `self.lstbx_mpf.select_set(0)` to activate selection cursor. – Derek Oct 16 '21 at 00:47
  • The error is occurred at self.rename_entry_mpf.select_adjust(END). If I delete this line, the program works properly. But I want to highlight the file name when I click the file to rename. That's why I used select_adjust. With "select_adjust", when I click the file, the entry is created and the name of file is displayed and highlighted. Then I write anything, even though I pushed just return button, the error is occured. – racoonisracoonracoon Oct 16 '21 at 16:11
  • I've been trying to generate the error but haven't had any luck. So I deliberately changed `lstbx_mpf` to `lstbx_db` in your code like this `self.rename_entry_mpf.insert(0, self.lstbx_mpf.get(self.lstbx_db.curselection()))` which generated error `_tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number` – Derek Oct 17 '21 at 08:06
  • I solved the problem. After self.rename_entry_mpf.select_adjust(END) in def rename_mpf(self) curselection is deleted, but I don't know why. So I added self.cur_sel_mpf = self.lstbx_mpf.curselection() right before the self.rename_entry_mpf.select_adjust(END) and modified os.rename(self.lstbx_mpf.get( self.lstbx_mpf.curselection()), self.rename_entry_mpf.get()) to os.rename(self.lstbx_mpf.get(self.cur_sel_mpf), self.rename_entry_mpf.get()). Maybe it's version problem. You said you are ok with my code. My version is 3.8.6 32-bit. What's yours? – racoonisracoonracoon Oct 19 '21 at 13:19
  • I'm using v3.9.1:1 – Derek Oct 19 '21 at 13:49

0 Answers0