Home > Net >  Getting error while saving context of a file
Getting error while saving context of a file

Time:08-06

I keep getting an error saying that the button failed to call the function when I press it here is the function it's failing to run

import re
from tkinter import *
from tkinter import ttk
import time
from turtle import bgcolor
import webbrowser
from tkinter import messagebox
from tkinter import filedialog

root = Tk()
root.title('Notepad')
root.iconbitmap('C:/Users/Hero/Documents/Visual Studio code/My project/notes.ico')
root.geometry("640x500")

root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")

global x
x = 20

def change_theme():
    if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
        root.tk.call("set_theme", "light")
    else:
        root.tk.call("set_theme", "dark")

def increase_font_size():
    global x
    global f
    x = x   1
    print(x)
    text.config(font=(f, x))
    
def decrease_font_size():
    global x
    global f
    x = x - 1
    print(x)
    text.config(font=(f, x))

def pfonts():
    font_page = Tk()
    font_page.title('Fonts')
    font_page.geometry('295x200')
    font_page.configure(bg='#292929')

    font_page.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
    font_page.tk.call("set_theme", "dark")

    global f

    fentry1 = ttk.Entry(font_page, width=80)
    fentry1.pack()
    label1 = Label(font_page, text='Enter the font name you want', font=("Tekton Pro", 17), bg='#292929')
    label2 = Label(font_page, text='warning: caps lock sensitive', font=("Tekton Pro", 8), bg='#292929')

    label1.pack()
    label2.pack(side=BOTTOM)

    def change_font():
        global f

        f = fentry1.get()
        text.config(font=(f, x))

        print(f)

    bbfont = ttk.Button(font_page, text='Confirm font', command=change_font)
    bbfont.pack(side=BOTTOM)

def killer():
    print("killing the instence")
    root.destroy()

def savefile():

    global filename

    filename = filedialog.asksaveasfilename(initialdir='/', title='Save File', filetypes=(('Text Files', '*.txt'), ('All Files', '*.*')))
    textContent = text.get("1.0",END)
    textContent = str(textContent)
    myfile = open(filename, "w ")
    myfile.write(textContent)

style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")

barframe = Frame()
barframe.pack(side=BOTTOM)

bfonts = ttk.Button(barframe, text='Fonts', style='Accent.TButton', command=pfonts)

open = ttk.Button(barframe, text='Open', style='Accent.TButton')

save = ttk.Button(barframe, text='Save', style='Accent.TButton', command=savefile)

close = ttk.Button(barframe, text='Close', style='Accent.TButton', command=killer)

#fonts = ttk.Entry(barframe, width=10)
calculater = ttk.Button(barframe, text='Calculater', width=10, style='Accent.TButton')

calander = ttk.Button(barframe, text='Calander', width=10, style='Accent.TButton')

increase_size = ttk.Button(barframe, text=' ', width=2, command=increase_font_size)

decrease_size = ttk.Button(barframe, text='-', width=2, command=decrease_font_size)

bfonts.grid(row=0, column=0)

open.grid(row=0, column=1)

save.grid(row=0, column=2)

close.grid(row=0, column=3)

#fonts.grid(row=0, column=1)
calculater.grid(row=0, column=4)

calander.grid(row=0, column=5)

increase_size.grid(row=0, column=6)

decrease_size.grid(row=0, column=7)

scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')

text=Text(root, font=("Georgia", x), yscrollcommand=scroll.set, bg='#292929', height=1, width=1)
scroll.config(command=text.yview)
text.pack(fill=BOTH, expand=1)

root.mainloop()

Error code

Traceback (most recent call last):
  File "C:\Users\Hero\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\Hero\Documents\Visual Studio code\My project\MainFrame.py", line 82, in savefile
    myfile = open(filename, "w ")
TypeError: 'Button' object is not callable

thats everything the whole script and the whole error i hope that helps i just thought it would get in the way if i posted the whole code as its really big lol

CodePudding user response:

After testing it, the problem was you assigning a predefined python variable to the open button. Where you have:

open = ttk.Button(barframe, text='Open', style='Accent.TButton')

Just change that to something like:

openButton = ttk.Button(barframe, text='Open', style='Accent.TButton')

And then downstream change to this:

openButton.grid(row=0, column=1)
  • Related