-1

I'm trying to make a GUI for a password logger program. Right now I'm only trying to learn how making a GUI works but only ten lines in, My program wont run because of a supposed syntax error.

import tkinter as tk
from tkinter import filedialog, Text
import os

root = tk.Tk()

canvas = root.Canvas(root, height=700, width=700, bg="#263d42")
canvas.pack()

root.mainloop()

This is the entire script. The error looks like this:

File "<stdin>", line 1 & c:/FileLocation/main.py"
                                              ^
SyntaxError: invalid syntax

I've tried removing unnecessary imports but I get the same issue.

I am only trying to get the program to display a canvas on the screen.

Absytes
  • 1
  • 1

5 Answers5

2

Looks like your problem is that you are trying to run python main.py from within the Python interpreter, which is why you're seeing that traceback.

Make sure you're out of the interpreter:

exit()

Then run the python main.py command from bash or command prompt or whatever.

CanciuCostin
  • 1,773
  • 1
  • 10
  • 25
2

Invoke python scripts like this:

PS C:\Users\sween\Desktop> python ./a.py

Not like this:

PS C:\Users\sween\Desktop> python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ./a.py
  File "<stdin>", line 1
    ./a.py
    ^
SyntaxError: invalid syntax

The three arrows >>> indicate a place to write Python code, not filenames or paths.

Dennis
  • 2,249
  • 6
  • 12
1

First thing I noticed was you need to switch out root.Canvas with tk.Canvas.

import tkinter as tk
from tkinter import filedialog, Text
import os

root = tk.Tk()

canvas = tk.Canvas(root, height=700, width=700, bg="#263d42")
canvas.pack()

root.mainloop()

Although even using your original unedited script, it didn't result in a SyntaxError, but an AttributeError for the canvas. I'm working from Pycharm, my assumption is you're working from command line? It looks like you're running main.py from within the interpreter, which you should be able to use exit() to resolve. The post linked here goes into more detail on that.

DWR Lewis
  • 470
  • 4
  • 15
0

For better assistance, you may need to provide your Python version.

For Python 3.8, use from tkinter import Tk or from tkinter import *.

If this did not solve your problem, you may have problem with tkinter installation.

0

Your syntax error is on line 6:

Instead of:

canvas = root.Canvas(root, height=700, width=700, bg="#263d42")

Try:

canvas = Canvas(root, height=700, width=700, bg="#263d42")