简体   繁体   中英

Using OOP to make Tkinter GUI

I am new to programming so I've taken a simple online project and tried to use OOP to make the GUI for it, can you help me with this error:

'RuntimeError: Too early to run the main loop: no default root window'

I took out the buttons to make it shorter:

from tkinter import *
import tkinter as tk
from tkinter import ttk

contact_list = [
    ['Dermot Bruce', '071 0403 6313'],
    ['Felix Kent', '071 7050 4862'],
    ['Eren Yeager', '071 4174 1560'],
    ['Roy Mustang'  '071 5173 4259'],
    ['Arietta Curtis', '071 4415 8004'],
    ['Jennifer Love', '071 8857 1196'],
]


class address_list():
    def __init__(self, *args):
        self.contact = contact_list


class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self, address_list)
        self.title = "Address Book Interface"
        self.geometry('400x400')
        self.resizable(0, 0)
        self.config(bg='lightblue')

        self.name = StringVar()
        self.number = StringVar()
        # create frame
        self.frame = Frame(self)
        self.frame.pack(side=RIGHT)

        def add_contact(contact_list):
            contact_list.append([self.name.get(), self.number.get()])
            select_set()

        # to view selected contact(first select then click on view button

        def edit(contact_list):
            contact_list[Selected()] = [self.name.get(), self.number.get()]
            select_set()

        def view(contact_list):
            NAME, PHONE = contact_list[Selected()]
            self.name.set(NAME)
            self.number.set(PHONE)

        # to delete selected contact

        def delete(contact_list):
            del contact_list[Selected()]
            select_set()

        # empty name and number field

        def reset(contact_list):
            self.name.set('')
            self.number.set('')

        # exit game window

        def exit(contact_list):
            self.destroy()

        def select_set(contact_list):
            contact_list.sort()
            select.delete(0, END)
            for name, phone in contact_list:
                enter
                code
                hereselect.insert(END, self.name)

        select_set()


if __name__ == '__main__':
    mainloop()

This error means exactly what it says. You can't call mainloop until you've created a root window, and you never create any widgets.

Your code should look like this:

if __name__ == '__main__':
    app = App()
    app.mainloop()

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