如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白

加入好友
加入社群
如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

from tkinter import Tk, Button, filedialog

# tkinter為全小寫,Tk, Button首字大寫

import pandas as pd

fpath = “”
def browse_file():
global fpath #兩個自定義函式都要用
fpath = filedialog.askopenfilename()
print(“已選擇檔案: \n”,fpath)
process_button.config(state=”normal”)

def process_file():
#fpath = filedialog.askopenfilename()
df=pd.read_csv(fpath,skip_blank_lines = True)
print(“已經讀入DataFrame:\n”,df)

root=Tk()

# root.geometry(“300×100”) #可以將視窗放大

browse_button = Button(root,text=”請選擇檔案”,command=browse_file)
#browse_file 非browse_file() #只要function name,不要()

#Button(root, text=”按鈕文字”, font=(“Arial”, 20), width=10, height=2)

#放大icon跟字型

browse_button.pack()
#若按鈕很多的話,使用.grid(row=0,column=0, stick=W)為佳
#stick = E W S N (東西南北)

process_button = Button(root,text=”執行程式”,command=process_file)
#process_file 非process_file()
process_button.pack()

root.mainloop()

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

GUI:

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

test.txt:

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

執行結果:

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

pd.read_csv(fpath, skip_blank_lines = True)

可以濾掉空列,Tab , 不定數空白

 

process_button.config(state=”normal”)

這段程式碼是將名為 process_button 的按鈕元件的狀態設為 normal,使按鈕變成可按狀態。

在 tkinter 中,元件的 state 屬性可以設定為以下幾種值:

  • normal:元件是可用的,可以被按下。
  • disabled:元件是不可用的,不能被按下。
  • readonly:元件是唯讀的,不能被編輯或改變。
  • active:元件是處於活動狀態的,例如 RadioButton 或 CheckButton 被選中時。

因此,當我們想要讓按鈕元件在某些事件發生後才變成可按狀態時,可以利用 config() 方法來設定元件的 state 屬性。

 

root.mainloop() 是 Tkinter 中必要的語句,它讓視窗進入主循環(main loop)並等待用戶事件。在此期間,系統會一直檢查事件,例如用戶是否點擊了某個按鈕或鍵盤是否有輸入。如果有事件發生,則相應的處理程序會被調用。在主循環中,你的程式會持續運行,直到你關閉了窗口或停止了主循環。因此,如果你的程式有圖形界面,那麼最後必須要呼叫 root.mainloop() 才能讓它正確地運行。

推薦hahow線上學習python: https://igrape.net/30afN

使用了全域變數fpath,這可能會導致一些問題。當您的程式碼變得更複雜時,使用全域變數可能會讓代碼難以理解和維護。一個更好的方式是將fpath變量作為browse_file()process_file()函數的參數傳遞

不使用全域變數的方法:

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

 

尚未選擇檔案之前,

執行程式的icon反白,不能作用

使用全域變數確實不是一個好的設計方式,但在某些情況下,例如需要在多個函數之間共享數據時,使用全域變數可能是一個比較簡單的解決方案。當然,也可以使用其他方法來實現這種功能,例如將變數作為函數的參數傳遞。不過,在這個情境下,使用全域變數確實是一個比較簡單的方式來實現按鈕在沒有選擇檔案之前不能點擊的功能。

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

推薦hahow線上學習python: https://igrape.net/30afN

可以使用一個物件導向的方式,將選擇檔案和處理檔案的功能封裝在一個類別中。這樣的話,就可以在類別中定義屬性和方法,並且不需要使用全域變數。

以下是一個簡單的範例:

from tkinter import Tk, Button, filedialog
import pandas as pd

class FileHandler:
    def __init__(self, master):
        self.master = master
        self.master.geometry(“200×100”)
        self.fpath = “”

        self.browse_button = Button(self.master, text=”請選擇檔案”,
                                    command=self.browse_file)
        self.browse_button.pack()

        self.process_button = Button(self.master, text=”執行程式”,
                                     command=self.process_file,
                                     state=”disabled”)
        self.process_button.pack()

    def browse_file(self):
        self.fpath = filedialog.askopenfilename()
        print(“已選擇檔案:\n”, self.fpath)
        self.process_button.config(state=”normal”)

    def process_file(self):
        df = pd.read_csv(self.fpath, skip_blank_lines=True)
        print(“已經讀入DataFrame:\n”, df)

root = Tk()
app = FileHandler(root)
root.mainloop()

如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

 
 def __init__(self, master):
    self.master = master
外部輸入一個master參數
class內部用self.master這個變數承接
因為self.master 只在
__init__(self, master):
的縮排之下
沒有其他函式用到了
如果少寫self.master = master
意即不用self.master這個變數承接
外部輸入的master
__init__() 縮排下
都可以直接用master這個變數
不用self.master
但若class中的其他function
也需要用到master這個變數
這寫法就不一定方便

加入好友
加入社群
如何用tkinter製作Python的GUI(圖形使用者介面)? from tkinter import Tk, Button, filedialog ; 物件導向避免使用全域變數 ; pandas.read_csv(fpath, skip_blank_lines = True) 可以濾掉空列,Tab , 不定數空白 - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *