欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python正交表运用

发布时间:2024/8/1 python 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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正交表运用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。