二进制搜索Python图形

时间:2018-11-11 07:07:03

标签: python python-3.x tkinter

我正在研究一个名为: “模拟器,以图形方式显示线性和二进制搜索。此模拟器要求您选择数组的大小,然后以图形方式显示该框的数量,然后可以将数据插入到这些框中。它仅允许您将数据插入第一个索引首先,它使第二个框能够插入数据,如此等等。然后询问用户要搜索哪些数据,并以图形方式显示搜索过程并最终显示结果。”

我已经编写了一个二进制搜索功能,但是它仅显示Mid的结果并且可以正常工作。只有帮助我,并帮助我显示算法的过程,它会更改标签的颜色,该元素是二进制搜索的元素检查。

import tkinter as tk

root=tk.Tk()
root.title("Linear & Binary Search")
root.geometry("1200x600")

box_list = []   # Create list of Entrys

#Functions
def binarySearch():
    index_list = []
    global snum, box_list
    x = snum.get()

    l=len(box_list)
    r=len(box_list)-1

    if r >= l: 

        mid = l + (r - l)/2

        if box_list[mid] == x: 
            index_list.append(mid)

        elif box_list[mid] > x: 
            return binarySearch(box_list, l, mid-1, x) 

        else: 
            return binarySearch(box_list, mid+1, r, x)



    #Print Area
    temp0=tk.Frame(root)
    temp0.pack()
    tk.Label(temp0,text="\n").pack()

    Result=tk.Frame(root)
    Result.pack()

    tk.Label(Result,text="Searched for  ",
                   font="Arial 10 bold",fg="blue",pady="10",
                   anchor="w").grid(row=0,column=0)
    tk.Label(Result, text=snum.get(),
             font="Arial 12 bold",bg="green",
             fg="white",bd="5",justify="left",
             padx="25",pady="25",
             relief="groove").grid(row=0,column=1)
    tk.Label(Result,text="  Result  ",
             font="Arial 10 bold",fg="blue",
             pady="10",anchor="w").grid(row=0,column=2)
    print(index_list)
    tk.Label(Result,text=index_list,
             font="Arial 12 bold",bg="green",
             fg="white",bd="5",justify="left",
             padx="25",pady="25",
             relief="groove").grid(row=0,column=3)

def ApplytoLabel():
    global snum, box_list
    xx=size.get()
    for i in range(xx):
        if box_list[i].cget('state') == 'normal':
            element = box_list[i].get() # Get value from corresponding Entry
            ArrayLabel=tk.Label(OutputArray,text=element,
                                font="Arial 12 bold",
                                bg="red",fg="white",
                                bd="5",justify="left",
                                padx="25",pady="25",
                                relief="groove")
            ArrayLabel.pack(side="left")
            box_list[i].configure(state='disabled')
            try:
                box_list[i+1].configure(state='normal')
            except IndexError: pass
            break


def Boxes():
    xx=size.get()
    for i in range(xx):        
        box=tk.Entry(InputArray,font="Arial 10 bold",bd="5",width="5",state='disabled' if i else 'normal')
        box.pack(side="left")
        box_list.append(box)
    ApplytoLabel1=tk.Button(InputArray,text="Submit To Array",command=ApplytoLabel).pack()


#Size of Array
Array = tk.Frame(root)
Array.pack()

text1=tk.Label(Array,text="Enter the Size of Array:",
               font="Arial 10 bold",fg="blue",
               pady="10",anchor="w").grid(row=0,column=0)

size=tk.IntVar()

ArraySize=tk.Entry(Array,textvariable=size,
                   font="Arial 10 bold",
                   bd="5").grid(row=0,column=1,sticky="w")
SizeofArray=tk.Button(Array,text="Submit",
                      command=Boxes).grid(row=0,column=2,sticky="w")




#Search Element
text2=tk.Label(Array,text="Number to search:",
               font="Arial 10 bold",fg="blue",pady="10",anchor="w")
text2.grid(row=2,column=0)
snum = tk.IntVar()
SearchNum=tk.Entry(Array,textvariable=snum,font="Arial 10 bold",bd="5")
SearchNum.grid(row=2,column=1)

BinarySearch=tk.Button(Array,text="Binary Search",command=binarySearch)
BinarySearch.grid(row=2,column=3)
#Search Element End


temp0=tk.Frame(root)
temp0.pack()
Space=tk.Label(temp0,text="\n")
Space.pack()

InputArray=tk.Frame(root)
InputArray.pack()

temp0=tk.Frame(root)
temp0.pack()
tk.Label(temp0,text="\n \n").pack()


OutputArray=tk.Frame(root)
OutputArray.pack()



root.mainloop()

0 个答案:

没有答案