作為一個不是計算機行業的工科生,領導最近希望做個GUI自動處理EXCEL資料,并實作資料處理,達到預期效果,這里把程序和代碼一步一步分享給大家,本人水平有限,也請大佬指導。
今天主要介紹GUI的界面,本人查找了大量資料,覺得TKinter適合新手學習,就采用這種方法,后續如果試了其他方法再貼出來大家討論。在GUI界面上根據要求主要包含:檔案路徑選擇、檔案合并、檔案結果展示等幾個方面,個人覺得難點主要在為文本框創建滾動條這里,代碼如下:
界面如下:

代碼如下:
#!/usr/bin/env python
# coding: utf-8
# In[102]:
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from tkinter import PhotoImage
from tkinter.filedialog import *
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as pt
# get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib as mpl
import xlrd,xlwt
import openpyxl
from mpl_toolkits.axisartist.axislines import SubplotZero
from matplotlib.pyplot import MultipleLocator
mpl.rcParams['font.sans-serif']=['SimHei'] #指定默認字體 SimHei為黑體
mpl.rcParams['axes.unicode_minus']=False #用來正常顯示負號
class Application(Frame):
"""一個經典的GUI程式"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.grid() #采用grid布置方式
# self.columnconfigure(0, minsize=2)
self.createWidget()
def createWidget(self):
"""創建組件"""
self.btn01 = Button(self,text="檔案選擇",width=10,height=2,
fg="green")
self.btn01.grid(row=0,column=0)
self.v1=StringVar()
self.entry01 = Entry(self,textvariable=self.v1)
self.entry01.grid(row=0,column=1)
self.entry01.delete(0, END) # 將輸入框里面的內容清空
self.entry01.insert(0, '選擇檔案路徑')
self.btn02 = Button(self,text="檔案合并",width=10,height=2,
fg="red")
self.btn02.grid(row=2,column=0)
self.label01 = Label(self,text = "請選擇熱害引數",width=15,height=2)
self.label01.grid(row=4,column=0)
self.name = StringVar()
self.combox = ttk.Combobox(self,textvariable=self.name)
self.combox["state"] = "readonly"
# self.combox.current(0)
self.combox.bind("<<Comboxselected>>")
self.combox.grid(row=4,column=1)
self.label01 = Label(self,text = "請選擇車速引數",width=15,height=2)
self.label01.grid(row=4,column=2)
self.name01 = StringVar()
self.combox01 = ttk.Combobox(self,textvariable=self.name01)
self.combox01["state"] = "readonly"
# self.combox.current(0)
self.combox01.bind("<<Comboxselected>>")
self.combox01.grid(row=4,column=3)
self.btn03 = Button(self,text="開始計算",fg="blue")
self.btn03.grid(row=8,column=0)
self.label02 = Label(self,text="比例結果",fg="red")
self.label02.grid(row=10,column=0)
self.btn03 = Button(self, text="結果清空", fg="green", command=self.delete)
self.btn03.grid(row=11, column=0)
self.label03 = Label(self, text="中低速", fg="black")
self.label03.grid(row=10, column=1)
self.scrollbar = Scrollbar(self,orient="vertical")
self.scrollbar.grid(row=11,column=2,sticky=S + W + N)
self.listbox = Listbox(self,height=6,width=20,yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox.yview)
self.listbox.grid(row=11,column=1,sticky=S + W + E + N)
self.label04 = Label(self, text="變速爬坡", fg="black")
self.label04.grid(row=10, column=3)
self.scrollbar01 = Scrollbar(self, orient="vertical")
self.scrollbar01.grid(row=11, column=4, sticky=S + W + N)
self.listbox01 = Listbox(self, height=6, width=20, yscrollcommand=self.scrollbar01.set)
self.scrollbar01.config(command=self.listbox01.yview)
self.listbox01.grid(row=11, column=3, sticky=S + W + E + N)
self.label05 = Label(self, text="城市工況", fg="black")
self.label05.grid(row=10, column=5)
self.scrollbar02 = Scrollbar(self, orient="vertical")
self.scrollbar02.grid(row=11, column=6, sticky=S + W + N)
self.listbox02 = Listbox(self, height=6, width=20, yscrollcommand=self.scrollbar02.set)
self.scrollbar02.config(command=self.listbox02.yview)
self.listbox02.grid(row=11, column=5, sticky=S + W + E + N)
self.label06 = Label(self, text="高速爬坡", fg="black")
self.label06.grid(row=10, column=7)
self.scrollbar03 = Scrollbar(self, orient="vertical")
self.scrollbar03.grid(row=11, column=8, sticky=S + W + N)
self.listbox03 = Listbox(self, height=6, width=20, yscrollcommand=self.scrollbar03.set)
self.scrollbar03.config(command=self.listbox03.yview)
self.listbox03.grid(row=11, column=7, sticky=S + W + E + N)
self.label07 = Label(self, text="高速超速", fg="black")
self.label07.grid(row=10, column=9)
self.scrollbar04 = Scrollbar(self, orient="vertical")
self.scrollbar04.grid(row=11, column=10, sticky=S + W + N)
self.listbox04 = Listbox(self, height=6, width=20, yscrollcommand=self.scrollbar04.set)
self.scrollbar04.config(command=self.listbox04.yview)
self.listbox04.grid(row=11, column=9, sticky=S + W + E + N)
self.btn04 = Button(self,text="已合并")
self.btn04.grid(row=2,column=1)
self.btn05 = Button(self, text="最大最小值計算", fg="black")
self.btn05.grid(row=7, column=0)
self.scrollbar05 = Scrollbar(self,orient="vertical")
self.scrollbar05.grid(row=7,column=2,sticky=S + W + N)
self.listbox05 = Listbox(self,height=6,width=20,yscrollcommand=self.scrollbar05.set)
self.scrollbar05.config(command=self.listbox.yview)
self.listbox05.grid(row=7,column=1,sticky=S + W + E + N)
#有需要時清空串列框
def delete(self):
self.listbox.delete(0,END)
self.listbox01.delete(0, END)
self.listbox02.delete(0, END)
self.listbox03.delete(0, END)
self.listbox04.delete(0, END)
self.listbox05.delete(0, END)
if __name__ == '__main__':
root = Tk()
root.title("一個經典GUI測驗程式")
root.geometry("1000x500")
app = Application(master=root)
root.mainloop()
程式略顯臃腫,有大神可以指點一二嗎?也希望對新人同學有所幫助
歡迎大家加QQ交流:475621704
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/243004.html
上一篇:TypeError: 'in <string>' requires string as left operand, not bytes
下一篇:scrapy爬蟲路徑
