-1
我正在做一個Python項目的接口,其中包括編譯一個可以編譯彙編語言程序的程序。我決定使用文本框和3個文本框進行界面:輸入1,錯誤顯示1,輸出1。 我還需要做一些基本的按鈕,比如創建一個新文件,在第一個文本框中打開一個,從第一個文本框中保存一個並進行編譯。我用菜單去做在Tkinter中打開保存文件
我的問題是,當我定義保存我的文件的功能(打開同樣的問題)時,即使我沒有單擊保存文件按鈕,編譯時也會保存。另外,當我正確保存文件時,我無法再修改文本框中的文本,有人可以解釋我爲什麼以及如何執行它?
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()