首先GUI中不同函數的局部變量的問題。
發現不同button定義的函數得到的變量無法通用。
通過global 函數內的變量可以解決這個問題
1 def openfiles2(): 2 global s2fname 3 s2fname = filedialog.askopenfilename(title='打開S2文件', filetypes=[('S2out', '*.out'), ('All Files', '*')]) 4 text.insert(tkinter.END, 'S2打開成功\n'+s2fname+'\n') 5 def openfilecgns(): 6 global cgnsfname 7 cgnsfname = filedialog.askopenfilename(title='打開CGNS文件',filetypes=[('CGNSdat', '*.dat'), ('All Files', '*')] ) 8 text.insert(tkinter.END, 'CGNS dat 打開成功\n'+cgnsfname+'\n') 9 def show():
-Text文本框的定義和輸出
定義:
text=tkinter.Text(root,width=20,height=20)
text.pack(fill=tkinter.X,side=tkinter.BOTTOM )
用到fill 可以填充某個方向,這次填充X方向,使得界面更好看。 side可以放置在不同的位置。但是這次用起來很多錯誤。
以后可以嘗試不用import tkinter 可以用from tkinter import * 雖然更危險,但是網上大部分的代碼都是這樣導出的。更容易參考。
實時更新並查看:
1 text.insert(tkinter.END, 'this Row finished...\n') # INSERT表示在光標位置插入 2 text.see(tkinter.END) 3 4 text.update()
如果不update就是 等到計算函數計算完之后才可以得到新的insert的數據。
用update可以解決這個問題。
利用see(END) 解決文本框滾動的問題。自動顯示最新的一行信息。類似於各種商業軟件求解器。
