1. 程式人生 > >python3 tkinter 桌面軟件教程

python3 tkinter 桌面軟件教程

ida date() inf edt name dia AS .get int

效果圖

技術分享圖片

""""brid布局"""
from tkinter import *
import tkinter.filedialog
from tkinter import scrolledtext        # 導入滾動文本框的模塊
from tkinter.messagebox import *
import time

class TKMain():
    xin = Tk()
    path = StringVar()
    #路徑賦值
    def selectPath(self):
        path_ = tkinter.filedialog.askopenfilename()
        self.path.set(path_)
    
# 桌面 def views(self): self.xin.geometry(400x400) # 指定主框體大小 self.xin.title(‘名稱) # 標題名 Label(self.xin, text="編號").grid(row=1, column=0, sticky=E) self.number_id = tkinter.StringVar() self.number_id_entry = tkinter.Entry(self.xin) self.number_id_entry[
"textvariable"] = self.number_id self.number_id_entry.grid(row=1, column=1, sticky=E) Label(self.xin, text="文件").grid(row=3, column=0, sticky=E) #Entry(self.xin, stat="readonly", textvariable=self.path).grid(row=3, column=1, sticky=E) self.xls_path = tkinter.StringVar() self.xls_path_entry
= tkinter.Entry(self.xin,stat="readonly") self.xls_path_entry["textvariable"] = self.path self.xls_path_entry.grid(row=3, column=1, sticky=E) Button(self.xin, text=" 選擇 ", command=self.selectPath).grid(row=3, column=2, sticky=E) # 占位 Label(self.xin, text="").grid(row=5, column=0) Button(self.xin, text=" 運行 ", command=self.validate).grid(row=6, column=1, sticky=E) Button(self.xin, text=" 停止 ").grid(row=6, column=2, sticky=E) # 占位 Label(self.xin, text="").grid(row=7, column=0) # 滾動文本框 scrolW = 50 # 設置文本框的長度 scrolH = 18 # 設置文本框的高度 self.text = scrolledtext.ScrolledText(self.xin, width=scrolW, height=scrolH, wrap=tkinter.WORD) self.text.grid(row=10, columnspan=8, sticky=tkinter.E) pass #驗證事件 def validate(self): is_validata = True; try: number_id_val = int(self.number_id.get()); xls_path_val = self.path.get() if xls_path_val =="": is_validata = FALSE if number_id_val < 1: is_validata = FALSE except: is_validata = FALSE if is_validata == FALSE: for i in range(0,100): self.text.see(END) # 一直查看文本的最後位置~ time.sleep(0.5) self.text.insert(end, "驗證不通過,請查看!" + str(i)+ "\r\n") self.text.update()#一直更新輸出 if i == 50 : break print("驗證不通過!") pass def app(self): self.views() mainloop() def main(): TKMain().app(); pass; if __name__ == "__main__": main()

python3 tkinter 桌面軟件教程