-1

I am doing the interface of a project in Python that consists in making a program that can compile an program for assembly language. I decided to use textboxs and to do an interface with 3 textboxs : 1 for the input, 1 for the error displays and 1 for the output. I have also to do some basic buttons, like to make a new file, to open one in the first textbox, to save one from the first textbox and to compile. I use a menu to do it

My problem is that when I define the function to save my file (same problem for open), it saves when I compile even if I didn't click on the savefile button. Also, when I correctly saved the file, I can't modify the text on my textboxs anymore, can someone explain me why and how I should do it ?

from tkinter import *

class Interface(Frame):

def __init__(self, parent):
    Frame.__init__(self, parent)   
    self.parent = parent        
    self.initUI(parent)

def initUI(self, win):

    self.parent.title("Interface assembleur")
    self.pack(fill=BOTH, expand=1)
    menubar = Menu(self.parent)
    self.parent.config(menu=menubar)  
    sousmenu = Menu(menubar, tearoff= 0)
    menubar.add_cascade(label="Fichier", menu=sousmenu)
    #sousmenu.add_command(label="Nouveau fichier", command=self.newFile)
    #sousmenu.add_command(label="Ouvrir", command=self.openFile())
    sousmenu.add_command(label="Enregistrer sous", command=self.saveFile())
    menubar.add_command(label="Compiler", command=self.hello)

    S = Scrollbar(self)
    S.pack(side=RIGHT, fill= Y)
    #myString = StringVar()

    T2 = Text(self, height = 30, width = 26)
    T2.pack()
    T2.pack(side=RIGHT, fill = BOTH)
    T2.config(yscrollcommand=S.set)
    T2.insert(END,"Console de sortie")

    T3 = Text(self, height = 7.5, width = 30)
    T3.pack()
    T3.pack(side=BOTTOM, fill = BOTH)
    T3.config(yscrollcommand=S.set)
    T3.insert(END, "Console d'erreur")

    T = Text(self, height = 50, width = 30)
    T.pack()
    T.pack(side=TOP, fill = BOTH)
    T.config(yscrollcommand=S.set)
    T.insert(END, "Console d'entrée")


def newFile(self):
    self.T.delete(0, END)
    self.T2.delete(0, END)
    self.T3.delete(0, END)

def hello(self):
    print ("hello!")

def saveFile(self):
    filename = filedialog.asksaveasfilename(parent=self, initialdir = "/",
    title = "Enregistrer sous",filetype = (("Fichiers textes","*.txt"),("Tous les fichiers","*.*")))
    if filename:
        return open(filename, 'w')

def openFile(self):
    ftypes = [('Fichiers textes', '*.txt'), ('Tous les fichiers', '*')]
    dlg = filedialog.Open(self, filetypes = ftypes)
    fl = dlg.show()

    if fl != '':
        f = open(fl, "r")
        text = f.read()
        self.txt.insert(END, text)


def main():
    win = Tk()
    win.geometry("640x480+440+140")
    test = Interface(win).pack()
    win.mainloop()  

if __name__ == '__main__':
    main()  
M. Tertre
  • 1
  • 2

1 Answers1

0

When you assign a command to your menu item, the function self.saveFile() is called because of the parentheses.

Instead you should just pass the function handle command = self.saveFile without parentheses.

Josselin
  • 2,593
  • 2
  • 22
  • 35