1. 程式人生 > >用Python3+tkinter開發簡單圖形介面程式

用Python3+tkinter開發簡單圖形介面程式

起因在這裡. 由於此專案和漢字相關, 個人也想嘗試Python的圖形介面開發, 於是開始嘗試.

現在還處於原型狀態, 兩百多行程式碼都在一個檔案中. 幾個主要問題:

原始碼節選:

讀入csv源資料:

    def 讀入源資料檔案(self, 檔名):
        # 官方文件參考: https://docs.python.org/3/library/csv.html#module-contents
        with open(檔名, newline='') as 源資料檔案:
            源資料讀取器 = csv.reader(源資料檔案, delimiter=',')
            for 行 in 源資料讀取器:
                self.字元列表.append(行)

匯出csv檔案:

    def 匯出檔案(self):
        with open(常量_修改後檔案, 'w', newline='') as 目標檔案:
            寫檔案 = csv.writer(目標檔案, delimiter=',')
            for 字元 in self.字元列表:
                寫檔案.writerow(字元)
        print("修改儲存到: " + 常量_修改後檔案)

建立圖片顯示:

    # 顯示圖片, 參考: https://stackoverflow.com/questions/35024118/how-to-load-an-image-into-a-python-3-4-tkinter-window
    def 建立圖片顯示(self, 區域, 字型名, 位置):
        字型區 = Frame(區域)
        字型區.pack(side = 位置)
        字型提示 = Label(字型區, text = 字型名)
        字型提示.pack()
        圖片路徑 = 常量_圖片主目錄 + 常量_圖片路徑[字型名] + self.圖片子路徑
        print(圖片路徑)
        try:
            圖片 = PhotoImage(file=圖片路徑)
        except:
            print("找不到圖片")
            圖片 = PhotoImage()
        圖片顯示 = Label(字型區, image=圖片)
        圖片顯示.image = 圖片
        圖片顯示.pack()
        return 圖片顯示

 有需要Python學習資料的小夥伴嗎?小編整理【一套Python資料、原始碼和PDF】,感興趣者可以加學習群:548377875或者加小編微信:【mmp9972】反正閒著也是閒著呢,不如學點東西啦~~