user5514149
user5514149

Reputation:

Simple Python GUI program won't run, says RESTART

I'm trying to create a python program that pulls up a simple window that displays the text "Hello World?" I've imported tkinter and have created a class called MyGUI that should create a simple window. Then I create an instance of the MyGUI class. When I hit "F5" or run the programming after saving it, I get an error:

RESTART: C:....my filepath.....
>>>

Here is the code:

import tkinter

class MyGUI:
    def init (self):
        # Create the main window widget.
        self.main_window = tkinter.tk()


        # Create a Label widget containing the
        # text 'Hello World!'
        self.label = tkinter.Label(self.main_window, text="Hello World!")

        # Call the Label widget's pack method.
        self.label.pack()

        # Enter the tkinter main loop.
        tkinter.mainloop()

# Create an instance of the MyGUI class
my_gui = MyGUI()

What causes the "RESTART" error? Does where I save my .py file matter for this program?

Any help would be greatly appreciated. Thanks

Upvotes: 0

Views: 7042

Answers (1)

en_Knight
en_Knight

Reputation: 5381

The good news:

  • Your code works (in that it doesn't crash in python3, as is)!

The bad news:

  1. Your code doesn't do anything :( Your only function would raise an exception if called
  2. You have a code-unrelated problem

To resolve problem #1, change init to __init__ and tkinter.tk to tkinter.Tk()

__init__ is the function called by default on instance construction. The underscores are important if you want to override it. The other issue is just a typo.

You're broader problem is... broader. yes it matters where you save your file. If you don't save it in the place you are running python from, you need to supply an absolute path to it, or a relative path from the place you are running from. This is a broad topic, but pretty important and not too challenging. Maybe try here, or any python tutorial.

I don't know what type F5 does on your computer. I would not in general expect it to run python code. Are you in an IDE, then maybe it does run python code? Are you playing call of duty, because then it's more likely to lob a virtual grenade? F5 is app-dependent, probably not a universal binding on your machine

Upvotes: 1

Related Questions