python3-tkinter模組錯誤筆記-text,entry等找不到相應方法
阿新 • • 發佈:2019-02-01
錯誤日誌:
file_path = f_name.get()
AttributeError: 'NoneType' object has no attribute 'get'
程式碼:
f_name = tkinter.Entry().pack(side='left', expand=True, fill='both')
應該定義和佈局進行分開:
f_name = tkinter.Entry() f_name.pack(side='left', expand=True, fill='both')
簡單的gui文字編輯器學習程式碼:
import tkinter from tkinter.scrolledtext import ScrolledText top = tkinter.Tk() top.title('FileAction') def load(): file_path = f_name.get() print(file_path) with open(file_path, 'r', encoding='utf-8')as f: text.delete(0.0, 'end') text.insert('insert', f.read()) def save(): file_path = f_name.get() print(file_path) with open(file_path, 'w', encoding='utf-8')as f: f.write(text.get('1.0', 'end')) label = tkinter.Label(text='請輸入檔案路徑:').pack() text = ScrolledText() text.pack(side='bottom', expand=True, fill='both') f_name = tkinter.Entry() f_name.pack(side='left', expand=True, fill='both') b_save = tkinter.Button(text='save', command=save).pack(side='left') b_open = tkinter.Button(text='open', command=load).pack(side='left') top.mainloop()