這段代碼似乎給我一個錯誤,我的參數我不知道我在這段代碼中做了什麼錯誤。當試圖使用python 2.7在tkinter中運行二進制搜索時,如果我不使用gui只是行代碼,那麼程序工作正常。這是在tkinter gui python中出現錯誤的二進制搜索2.7
http://www.pythonschool.net/data-structures-algorithms/binary-search/
的代碼,我根據我的程序錯誤是:在Tkinter的回調回溯(最近通話最後一個)例外:文件「C:\ Python27 \ LIB \ LIB-TK \ Tkinter的。 PY」,線路1536,在調用返回self.func(*參數)類型錯誤:的binarySearch()到底需要2個參數(0給出)
from Tkinter import *
import csv
with open('scoresRecord.csv', 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
root = Tk()
root.resizable(0,0)
root.title("Score Board")
def binarySearch(myitem,myList):
found = False
bottom = 0
top = len(myList)-1
while bottom<=top and not found:
middle = (bottom+top)//2
if myList[middle]== myitem:
found = True
elif myList[middle] < myitem:
bottom = middle + 1
else:
top = middle-1
return found
if __name__=="__main__":
Scorelist = [row[0] for row in your_list]
Dismissallist =[row[1] for row in your_list] # create a list of only the dismissal
Venuelist =[row[2] for row in your_list] # create a list of only the venue
item = int(input(entScore.get()))
Scorelist = map(int, Scorelist)
rowPos = Scorelist.index(item)
isitFound = binarySearch(item,Scorelist)
if isitFound:
Score= Scorelist[rowPos]
Dismissal= Dismissallist[rowPos]
Venue= Venuelist[rowPos]
else:
Score= ("Not Found")
Dismissal= ("Not Found")
Venue= ("Not Found")
numScoreLbl.configure(text=Score)
numDismissal.configure(text=Dismissal)
outVenue.configure(text=Venue)
#==========GUI Section==================#
一個很好的開始就是修正你的縮進,以便它真正匹配你正在運行的代碼。縮進在Python中至關重要,而且您發佈的內容根本無法運行。 –