Code problem in python

my code is saying that there is indentation problems (don’t know how to fix)

Hey @EDWINBENNION, welcome to the forums!

It will be much more easier to help you if you could send us the link to your repl!

do i have to publish it first to share it?

You’ve probably mixed up tabs and spaces somewhere. In python3 everything must be either tabs, or spaces. Try putting your code through a tool like this one: https://tabstospaces.com/

it now says unindent does not match any outer indentation level

here is the link https://replit.com/@EDWINBENNION/code#main.py

Fixed:

import tkinter as tk
from tkinter import colorchooser
from tkinter import filedialog

root = tk.Tk()
txt = tk.Text(root)
txt.pack()

def change_color(color):
    txt.tag_configure("color", foreground=color)
    txt.tag_add("color", "sel.first", "sel.last")

color_button = tk.Button(root, text="Change Color", command=lambda: change_color("red"))
color_button.pack()

def choose_color():
    color = colorchooser.askcolor()
    change_color(color[1])
    choose_color_button = tk.Button(root, text="choose color", command=choose_color)
    choose_color_button.pack()

def open_file():
    filepath = filedialog.askopenfilename()
    with open(filepath, 'r') as f:
        content = f.read()
    txt.insert(tk.END, content)
    
def save_file():
    filepath = filedialog.asksaveasfilename(defaultextension='.txt')
    with open(filepath, 'w') as f:
        contents = txt.get("1.0",tk.END)
        f.write(content)

menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=open_file)
filemenu.add_command(label="Save", command=save_file)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)

root.mainloop()
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.