python正交表运用

1.首先制作一个简单的可视化界面,使用python自带库tkinter,代码如下

from tkinter.scrolledtext import ScrolledText
from tkinter import *
import tkinter as tk
import os


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()

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 re



def cancelMsg():          #删除功能
    scr.delete('0.0', END)
    scr1.delete('0.0', END)



def yunxin():      #运行
 try:
    global dop    #保存最终数据
    global doom
    doom=[]
    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":          #正交表存入完后跳出循环
                break
            if 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=0
        for i in counts.keys():
            sox=sox+counts[i]
        print(sox)
        for fo in doon:                        #将结果输出到输出框
            np=0
            dp=[]
            sx = 0
            for x in fo:
                sx = sx + 1
            print(sx)
            jo=0
            while 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+1
                np=np+1
            dop.append(dp)
        print(dop)
        scr1.delete('0.0', END)
        #scr1.insert(END, txtMsg1.get('0.0', END))
        sum1=1
        for jj in dop:
            scr1.insert(END,str(sum1)+':')
            scr1.insert(END,jj)
            scr1.insert(END, "\n")
            sum1=sum1+1
 except:
     oop = Tk()
     oop.title('运行失败')
     screenwidth = oop.winfo_screenwidth()
     screenheight = oop.winfo_screenheight()
     # 设置界面宽度为530,高度为365像素,并且基于屏幕居中
     width = 300
     height = 150
     size = '%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.active
     sheet.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 = 300
       height = 150
       size = '%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 = 420
 height = 120
 size = '%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,还望提醒,我好进一步改良代码,谢谢。

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值