2017-04-19 7 views
-1

私は、アセンブリ言語のためのプログラムをコンパイルできるプログラムを作ることにあるPythonでプロジェクトのインターフェースをやっています。テキストボックスを使用し、3つのテキストボックス(入力用に1つ、エラー表示に1つ、出力用に1つ)を使用することに決めました。 新しいファイルを作成し、最初のテキストボックスでファイルを開き、最初のテキストボックスからファイルを保存してコンパイルするなどの基本的なボタンも必要です。私はそれを行うためにメニューを使用するOpen Tkinterでファイルを開く

私の問題は、ファイルを保存するための関数を定義するとき(openと同じ問題)、savefileボタンをクリックしなくてもコンパイル時に保存するということです。また、ファイルを正しく保存しても、テキストボックスのテキストを修正することはできません。なぜ、どのようにすればよいのか説明できますか?

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() 

答えて

0

あなたは、メニュー項目にコマンドを割り当てると、機能self.saveFile()があるため、括弧で呼ばれています。

代わりに、かっこなしの関数ハンドルcommand = self.saveFileを渡すだけです。

関連する問題