0

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

1 Answers1

1

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

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
  • Thanks en_Knight. So now it works-ish. When I run the program it does now have a pop-up window that says "Hello World!" I guess I'm wondering what is causing the the "RESTART: C:....my filepath....." text to appear. I've looked through the Python 3.5 guide (https://docs.python.org/3/library/idle.html?highlight=restart) and could not find a definitive answer. –  Nov 18 '15 at 04:16
  • That's not typical. Are you using some special python build for educational purposes, or an IDE of some sort? – en_Knight Nov 18 '15 at 04:17
  • IDE and the normal build I downloaded from Python's website. –  Nov 18 '15 at 04:18
  • IDLE, I assume? (IDE just means integrated development environemnt, IDLE is specific to python and ships with it). I *think* that just means IDLE restarts itself or wants to... is Idle restarting itself? – en_Knight Nov 18 '15 at 04:19
  • So I just saved my gui.py file in the same folder that I'm running Python from and I'm still getting that "RESTART:....." error. –  Nov 18 '15 at 04:21
  • I tried it in both (Visual Studio with Python extensions and IDLE). In VS it runs perfectly (after implementing the code errors you caught), but its in IDLE that I'm getting the "RESTART:...." error. –  Nov 18 '15 at 04:24
  • ya it's just an idle thing then, don't worry about it. If it doesn't affect your code, it's not an error, just the IDE letting you know what it's thinking – en_Knight Nov 18 '15 at 04:34