python正交表运用
生活随笔
收集整理的這篇文章主要介紹了
python正交表运用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.首先制作一個簡單的可視化界面,使用python自帶庫tkinter,代碼如下
from tkinter.scrolledtext import ScrolledText from tkinter import * import tkinter as tk import osdop=[] doom=[] window = Tk() window.title("正交表()") # title() 定義這個窗口的標題 window.geometry("850x600") # geometry() 定義窗口的大小 srcLanguage = StringVar() srcLanguage.set(',') Label(window, text="內分隔符:",font=("隸書", 12)).place(x=680, y=170) languages = (',', ' ', ',', '.','。') ttk.Combobox(window, values=languages, width=6, textvariable=srcLanguage, state='readonly').place(x=760, y=170) Label(window, text="--輸入數據--",font=("隸書", 14)).place(x=270, y=1) scr = scrolledtext.ScrolledText(window, width=80, height=15, font=("隸書", 12)) # 滾動文本框(寬,高(這里的高應該是以行數為單位),字體樣式) scr.place(x=15, y=25) # 滾動文本框在頁面的位置 Label(window, text="--輸出結果為--",font=("隸書", 14)).place(x=265, y=270) scr1 = scrolledtext.ScrolledText(window, width=80, height=15, font=("隸書", 12)) # 滾動文本框(寬,高(這里的高應該是以行數為單位),字體樣式) scr1.place(x=15, y=300) # 滾動文本框在頁面的位置 srcLanguag = StringVar() srcLanguag.set(':') Label(window, text="項目分隔:",font=("隸書", 12)).place(x=680, y=80) language = (':', ';', ';', ':','>','<') ttk.Combobox(window, values=language, width=6, textvariable=srcLanguag, state='readonly').place(x=760, y=80) button2 = Button(window, text="運 行",bg="green",font=("隸書", 20), command=yunxin) #按鍵 button2.place(x=540, y=560, width=80, height=30) #按鍵位置 button3 = Button(window, text="導 出",bg="yellow",font=("隸書", 20), command=daocu) #按鍵 button3.place(x=290, y=560, width=80, height=30) #按鍵位置 button1 = Button(window, text="刪 除",bg="red", font=("隸書", 20), command=cancelMsg) #anjian button1.place(x=40, y=560, width=80, height=30) #按鍵位置 Label(window, text="測試用例",font=("隸書", 13)).place(x=700, y=310) Label(window, text="平臺:A,B,C",font=("隸書", 11)).place(x=695, y=340) Label(window, text="項目:E,F,G",font=("隸書", 11)).place(x=695, y=370) Label(window, text="環境:I,J,K",font=("隸書", 11)).place(x=695, y=400) Label(window, text="工具:M,N,O",font=("隸書", 11)).place(x=695, y=430) Label(window, text="操作提示",font=("隸書", 11)).place(x=705, y=480) Label(window, text="列數多的放后面",font=("隸書", 11)).place(x=680, y=510) #Label(window, text="列數多的放后面",font=("隸書", 11)).place(x=680, y=540) window.mainloop()2.代碼實現
from tkinter.scrolledtext import ScrolledText from tkinter import * import tkinter as tk import os from tkinter import ttk from tkinter import filedialog from tkinter.filedialog import askdirectory # 導入目錄操作庫 import tkinter.messagebox # 彈出消息框 import tkinter.filedialog # 導入操作文件的庫 from openpyxl import load_workbook import redef cancelMsg(): #刪除功能scr.delete('0.0', END)scr1.delete('0.0', END)def yunxin(): #運行try:global dop #保存最終數據global doomdoom=[]dop=[]doon = [] #保存正交表don = [] #輸入數據處理counts = dict() # 字典類型if scr.get('0.0', END) != "": #判定是否為空di = scr.get('0.0', END).split("\n") #進行行分割'''for jox in di:if jox!='':w_x=w_x+1'''#print(di)for i in di: #進行列分割if i !="":d=i.split(srcLanguag.get())doom.append(d[0])dm=d[1].split(srcLanguage.get())don.append(dm)counts[len(dm)] = counts.get(len(dm), 0) + 1 #字典類型保存print(don) #測試結果輸出print(counts) #測試結果輸出print(len(counts)) #測試結果輸出#print(counts.keys())fo = open("G:\\python\\zhen.txt", "r", encoding='UTF-8') #打開正交表文件m = 0 #開關按鈕for line in fo.readlines(): #按行輸入if m==1 and line!="\n":doon.append(line.split("\n")[0]) #保存正交表if m == 1 and line == "\n": #正交表存入完后跳出循環breakif m==0: #尋找合適正交表for no in counts.keys():#print(str(no)+'^'+str(counts[no]))if str(no)+'^'+str(counts[no]) in line:print(str(no) + '^' + str(counts[no]))m=1 #開關按鈕else:m=0 #開關按鈕break #關閉內部for循環print(doon) #測試結果輸出sox=0for i in counts.keys():sox=sox+counts[i]print(sox)for fo in doon: #將結果輸出到輸出框np=0dp=[]sx = 0for x in fo:sx = sx + 1print(sx)jo=0while jo<sx:if jo<sx-2*(sx-sox):dp.append(don[np][eval(fo[jo])])else:dp.append(don[np][eval(fo[jo:jo+2])])jo=jo+1#print(jo)jo=jo+1np=np+1dop.append(dp)print(dop)scr1.delete('0.0', END)#scr1.insert(END, txtMsg1.get('0.0', END))sum1=1for jj in dop:scr1.insert(END,str(sum1)+':')scr1.insert(END,jj)scr1.insert(END, "\n")sum1=sum1+1except:oop = Tk()oop.title('運行失敗')screenwidth = oop.winfo_screenwidth()screenheight = oop.winfo_screenheight()# 設置界面寬度為530,高度為365像素,并且基于屏幕居中width = 300height = 150size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)oop.geometry(size)label = Label(oop, text="---運行失敗,請檢查輸入---")label.grid(row=0, column=1, padx=65, pady=50)def daocu():def selectPath():try:contnt = Entry(op, textvariable=path, width=40).get()contnt = contnt.strip()#print(contnt)path.set(contnt)print(path.get())work = load_workbook(filename=str(path.get()) + '.xlsx')sheet = work.activesheet.append(doom)for x in dop:sheet.append(x)work.save(filename=str(path.get()) + '.xlsx')op.destroy()except:oop = Tk()oop.title('文件錯誤')screenwidth = oop.winfo_screenwidth()screenheight = oop.winfo_screenheight()# 設置界面寬度為530,高度為365像素,并且基于屏幕居中width = 300height = 150size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)oop.geometry(size)label = Label(oop, text="---請檢查表格是否存在---")label.grid(row=0, column=1, padx=65, pady=50)op = Tk()op.title('EXCEL表格名')screenwidth = op.winfo_screenwidth()screenheight = op.winfo_screenheight()width = 420height = 120size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)op.geometry(size)path = StringVar()Label(op, text="-自動退出代表保存成功-").place(x=140, y=27)Label(op, text="輸入文件名:").place(x=10, y=75)m=Entry(op, textvariable=path, width=40)m.place(x=90, y=75)# 操作按鈕n=Button(op, text="保存", command=selectPath, bg="green")n.place(x=380, y=73)dop=[] doom=[] window = Tk() window.title("正交表()") # title() 定義這個窗口的標題 window.geometry("850x600") # geometry() 定義窗口的大小 srcLanguage = StringVar() srcLanguage.set(',') Label(window, text="內分隔符:",font=("隸書", 12)).place(x=680, y=170) languages = (',', ' ', ',', '.','。') ttk.Combobox(window, values=languages, width=6, textvariable=srcLanguage, state='readonly').place(x=760, y=170) Label(window, text="--輸入數據--",font=("隸書", 14)).place(x=270, y=1) scr = scrolledtext.ScrolledText(window, width=80, height=15, font=("隸書", 12)) # 滾動文本框(寬,高(這里的高應該是以行數為單位),字體樣式) scr.place(x=15, y=25) # 滾動文本框在頁面的位置 Label(window, text="--輸出結果為--",font=("隸書", 14)).place(x=265, y=270) scr1 = scrolledtext.ScrolledText(window, width=80, height=15, font=("隸書", 12)) # 滾動文本框(寬,高(這里的高應該是以行數為單位),字體樣式) scr1.place(x=15, y=300) # 滾動文本框在頁面的位置 srcLanguag = StringVar() srcLanguag.set(':') Label(window, text="項目分隔:",font=("隸書", 12)).place(x=680, y=80) language = (':', ';', ';', ':','>','<') ttk.Combobox(window, values=language, width=6, textvariable=srcLanguag, state='readonly').place(x=760, y=80) button2 = Button(window, text="運 行",bg="green",font=("隸書", 20), command=yunxin) #按鍵 button2.place(x=540, y=560, width=80, height=30) #按鍵位置 button3 = Button(window, text="導 出",bg="yellow",font=("隸書", 20), command=daocu) #按鍵 button3.place(x=290, y=560, width=80, height=30) #按鍵位置 button1 = Button(window, text="刪 除",bg="red", font=("隸書", 20), command=cancelMsg) #anjian button1.place(x=40, y=560, width=80, height=30) #按鍵位置 Label(window, text="測試用例",font=("隸書", 13)).place(x=700, y=310) Label(window, text="平臺:A,B,C",font=("隸書", 11)).place(x=695, y=340) Label(window, text="項目:E,F,G",font=("隸書", 11)).place(x=695, y=370) Label(window, text="環境:I,J,K",font=("隸書", 11)).place(x=695, y=400) Label(window, text="工具:M,N,O",font=("隸書", 11)).place(x=695, y=430) Label(window, text="操作提示",font=("隸書", 11)).place(x=705, y=480) Label(window, text="列數多的放后面",font=("隸書", 11)).place(x=680, y=510) #Label(window, text="列數多的放后面",font=("隸書", 11)).place(x=680, y=540) window.mainloop()運行結果
本人新手,如有bug,還望提醒,我好進一步改良代碼,謝謝。
總結
以上是生活随笔為你收集整理的python正交表运用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue商城秒杀倒计时功能
- 下一篇: websocket python爬虫_p