Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Window Not Appearing
#12
OK,  It now deletes, and I cleaned up a few things.
From here on, you're on your own.
Enjoy:
import tkinter as tk
import os
import json
import tkinter.ttk as ttk
import tkinter.messagebox as tm


class ShoeBot:
    def __init__(self, parent, filename):
        self.roots = parent
        self.roots.title('AWD ShoeBot')
        # self.roots.geometry('400x200+10+10')
        self.treeheight = 19
        self.filename = os.path.abspath(filename)
        self.fp = None
        self.people = {}
        if os.path.isfile(self.filename):
            self.load_data()
        self.changes_made = False
        self.this_person = None
        self.name = tk.StringVar()
        self.shoe_size = tk.StringVar()
        self.build_gui()
        self.load_tree()

    def build_gui(self):
        self.create_main_frame()
        self.create_left_frame()
        self.create_right_frame()
        self.create_bottom_frame()
        self.create_tree()
        self.create_user_dialog()
        self.create_statusbar()
        self.roots.protocol("WM_DELETE_WINDOW", self.on_delete)

    def create_main_frame(self):
        self.main_frame = tk.Frame(self.roots, relief=tk.RAISED)
        self.main_frame.grid(row=0, column=0, sticky='nsew')
        self.main_frame.columnconfigure(0, weight=1)
        self.main_frame.rowconfigure(0, weight=1)
        self.main_frame.pack(pady=5, padx=5)

    def create_left_frame(self):
        self.left_frame = tk.Frame(self.main_frame, relief=tk.SUNKEN)
        self.left_frame.grid(row=0, rowspan=self.treeheight, column=0, columnspan=2, sticky='ns')

    def create_right_frame(self):
        self.right_frame = tk.Frame(self.main_frame, relief=tk.SUNKEN)
        self.right_frame.grid(row=0, rowspan=self.treeheight, column=2, columnspan=10, sticky='ns')

    def create_bottom_frame(self):
        self.bottom_frame = tk.Frame(self.main_frame, relief=tk.SUNKEN)
        self.bottom_frame.grid(row=self.treeheight + 1, column=0, columnspan=11, sticky='ew')

    def create_tree(self):
        treestyle = ttk.Style()
        treestyle.configure('Treeview.Heading', foreground='white',
                            borderwidth=2, background='SteelBlue',
                            rowheight=self.treeheight,
                            height=3)

        self.tree = ttk.Treeview(self.left_frame,
                                 height=self.treeheight,
                                 padding=(2, 2, 2, 2),
                                 columns='Name',
                                 selectmode="extended")

        self.tree.column('#0', stretch=tk.YES, width=180)
        self.tree.tag_configure('monospace', font='courier')
        treescrolly = tk.Scrollbar(self.left_frame, orient=tk.VERTICAL,
                                   command=self.tree.yview)
        treescrolly.grid(row=0, rowspan=self.treeheight, column=1, sticky='ns')

        treescrollx = tk.Scrollbar(self.left_frame, orient=tk.HORIZONTAL,
                                   command=self.tree.xview)
        treescrollx.grid(row=self.treeheight + 1, column=0, columnspan=2, sticky='ew')
        self.tree.configure(yscroll=treescrolly)
        self.tree.configure(xscroll=treescrollx)
        self.tree.grid(row=0, rowspan=self.treeheight, column=0, sticky='nsew')
        self.tree.bind('<Double-1>', self.name_selected)

    def clear_entry(self):
        self.nameE.delete(0, 'end')
        self.shoeSizeE.delete(0, 'end')

    def clear_tree(self):
        all_children = self.tree.get_children()
        for item in all_children:
            self.tree.delete(item)

    def load_tree(self):
        self.clear_tree()
        keys = list(self.people.keys())
        keys.sort()
        for key in keys:
            self.tree.insert('', 'end', text='{}'.format(key))

    def create_user_dialog(self):
        self.intruction = tk.Label(self.right_frame, text='Enter Information Below\n')
        self.intruction.grid(row=0, column=0, sticky='nse')
        self.nameL = tk.Label(self.right_frame, text='Name: ')
        self.shoeSizeL = tk.Label(self.right_frame, text='Shoe Size: ')

        self.nameL.grid(row=1, column=0, sticky='w')
        self.shoeSizeL.grid(row=2, column=0, sticky='w')

        self.nameE = tk.Entry(self.right_frame, textvariable=self.name)
        self.shoeSizeE = tk.Entry(self.right_frame, textvariable=self.shoe_size)
        self.nameE.grid(row=1, column=1)
        self.shoeSizeE.grid(row=2, column=1)

        signupButton = tk.Button(self.right_frame, text='Create', command=self.add_person)
        signupButton.grid(row=4, column=0, sticky='w')

        removeButton = tk.Button(self.right_frame, text='Delete', command=self.remove_person)
        removeButton.grid(row=4, column=1, sticky='w')

        self.spacer = tk.Label(self.right_frame, bd=0)
        self.spacer.grid(row=5, column=0)

    def create_statusbar(self):
        self.sb = tk.Frame(self.bottom_frame)
        self.sb.grid(row=0, column=0, sticky='nsew')

    def on_delete(self):
        if self.changes_made:
            self.save_data()
        self.roots.destroy()

    def load_data(self):
        with open(self.filename) as fp:
            self.people = json.load(fp)

    def save_data(self):
        with open(self.filename, 'w') as fo:
            json.dump(self.people, fo)

    def add_person(self):

        name = self.name.get()
        shoesize = self.shoe_size.get()
        if len(name) == 0 or len(shoesize) == 0:
            tm.showerror('Add Person', 'Missing data')
        else:
            self.people[name] = {'shoesize': shoesize}
            self.tree.insert('', 'end', text='{}'.format(name))
            self.changes_made = True
            self.clear_entry()
            msg = 'Added {}'.format(name)

    def remove_person(self):
        name = self.name.get()
        if len(name) == 0:
            tm.showerror('Remove Person', 'No Name')
        else:
            del self.people[name]
            print(f'People: {self.people}')
            self.changes_made = True
            self.load_tree()
        self.clear_entry()

    def name_selected(self, event):
        curitem = self.tree.focus()
        nidx = self.tree.item(curitem)
        name = nidx['text']
        shoesize = self.people[name]['shoesize']
        print('You selected {} who wears size {} shoe'.format(name, shoesize))
        # do stuff with selected person

    def find_person(self):
        try:
            person = self.name.get()
            self.this_person = self.people[person]
            print('found {} shoesize: {}'.format(self.this_person[name], self.this_person[name]['shoesize']))
        except:
            'Adding {} shoesize: {}'.format(self.name.get(), self.shoe_size.get())
            self.add_person()
            self.this_person = self.people[person]
        self.name.set('')
        self.shoe_size.set('')
        return self.this_person


def main():
    root = tk.Tk()
    sb = ShoeBot(root, filename='PeopleFile.json')
    root.mainloop()

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
Window Not Appearing - by MGallo - Sep-18-2017, 10:30 AM
RE: Window Not Appearing - by Lux - Sep-18-2017, 07:24 PM
RE: Window Not Appearing - by MGallo - Sep-18-2017, 10:05 PM
RE: Window Not Appearing - by Larz60+ - Sep-18-2017, 10:32 PM
RE: Window Not Appearing - by MGallo - Sep-18-2017, 10:40 PM
RE: Window Not Appearing - by Larz60+ - Sep-18-2017, 11:29 PM
RE: Window Not Appearing - by MGallo - Sep-19-2017, 02:11 AM
RE: Window Not Appearing - by Larz60+ - Sep-19-2017, 02:44 AM
RE: Window Not Appearing - by Larz60+ - Sep-19-2017, 07:49 PM
RE: Window Not Appearing - by Larz60+ - Sep-20-2017, 09:08 PM
RE: Window Not Appearing - by MGallo - Sep-20-2017, 11:21 PM
RE: Window Not Appearing - by Larz60+ - Sep-22-2017, 07:18 AM
RE: Window Not Appearing - by Larz60+ - Sep-26-2017, 07:52 PM
RE: Window Not Appearing - by Larz60+ - Sep-28-2017, 08:46 AM
RE: Window Not Appearing - by Larz60+ - Sep-30-2017, 07:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,957 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Graph is not appearing in correct frame? garynewport 2 1,128 Jan-11-2023, 11:55 AM
Last Post: garynewport
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,218 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Grid Line not appearing at 0 JoeDainton123 1 1,701 Oct-05-2020, 06:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020