当我单击Decimal to Binary按钮时,我收到的错误是ValueError: invalid literal for int() with base 10: ''
。
当我点击二进制到十进制按钮时,我也得到ValueError: invalid literal for int() with base 10: 'e'
。
以下是所有代码,以便您可以在转换函数本身之外查看错误的位置。对我需要修复的内容以及如何修复它的任何帮助都将非常感激。
import Tkinter
def convertDtoB(binary,left):
if(left>0):
binary+=str(left%2)
return convertDtoB(binary,left//2)
else:
binary = int(binary[::-1])
return binary
def convertBtoD(decimal):
answer = 0
length = len(str(decimal))
decimal2 = str(decimal)[::-1]
for i in range(length):
answer+=int(decimal2[i])*2**i
return answer
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.button1 = Tkinter.Button(self, text="Convert Decimal to Binary", command=lambda: self.OnButtonClick(1))
self.button1.grid(column=1,row=1)
self.button2 = Tkinter.Button(self, text="Convert Binary to Decimal", command=lambda: self.OnButtonClick(2))
self.button2.grid(column=1,row=2)
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label.grid(column=1,row=0,columnspan=2,sticky='')
self.labelVariable.set("Select a conversion.")
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
def OnButtonClick2(self, button_id):
if button_id == 3:
binaryNumber = convertDtoB("",self.decimalNumber)
self.labelVariable = Tkinter.StringVar()
label_2 = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label_2.grid(column=1,row=0,columnspan=2,sticky='')
self.label_2.set(binaryNumber)
elif button_id == 4:
binaryNumber = convertBtoD(self.binaryNumber)
self.labelVariable = Tkinter.StringVar()
label_2 = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label_2.grid(column=1,row=0,columnspan=2,sticky='')
self.label_2.set(binaryNumber)
def OnButtonClick(self, button_id):
if button_id == 1:
self.button1.destroy()
self.button2.destroy()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.decimalNumber = self.entryVariable.set("Enter Decimal Number Here")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
button3 = Tkinter.Button(self, text="Click here to convert", command=lambda: self.OnButtonClick(3))
button3.grid(column=1,row=1)
self.OnButtonClick2(3)
elif button_id == 2:
self.button1.destroy()
self.button2.destroy()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.binaryNumber = self.entryVariable.set("Enter Binary Number Here.")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
button4 = Tkinter.Button(self, text="Click here to convert", command=lambda: self.OnButtonClick(4))
button4.grid(column=1,row=1)
self.OnButtonClick2(4)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()
答案 0 :(得分:1)
代码中存在许多错误,其主要部分位于第65行至第68行,类似地位于第80行至第83行
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.decimalNumber = self.entryVariable.set("Enter Decimal Number Here")
这里设置输入框以使用变量self.entryVariable而不是self.decimalNumber。 StingVar对象的set方法是就地方法,并不返回任何有用的东西(它返回none作为所有inplace方法)。因此,self.decimalNumber变量实际上从不引用StringVar对象。以下是修复此代码和其他一些错误的代码.. XD
def OnButtonClick2(self, button_id):
if button_id == 3:
binaryNumber = convertDtoB("",int(self.entryVariable.get()))
self.labelVariable = Tkinter.StringVar()
label_2 = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label_2.grid(column=1,row=0,columnspan=2,sticky='')
self.labelVariable.set(binaryNumber)
elif button_id == 4:
binaryNumber = convertBtoD(self.entryVariable.get())
self.labelVariable = Tkinter.StringVar()
label_2 = Tkinter.Label(self,textvariable=self.labelVariable, anchor="center", bg="white")
label_2.grid(column=1,row=0,columnspan=2,sticky='')
self.labelVariable.set(binaryNumber)
def OnButtonClick(self, button_id):
if button_id == 1:
self.button1.destroy()
self.button2.destroy()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.entryVariable.set("Enter Decimal Number Here")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
button3 = Tkinter.Button(self, text="Click here to convert", command=lambda: self.OnButtonClick2(3))
button3.grid(column=1,row=1)
elif button_id == 2:
self.button1.destroy()
self.button2.destroy()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=1,row=0,sticky='EW')
self.entryVariable.set("Enter Binary Number Here.")
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
button4 = Tkinter.Button(self, text="Click here to convert", command=lambda: self.OnButtonClick2(4))
button4.grid(column=1,row=1)
答案 1 :(得分:0)
考虑这个电话
binaryNumber = convertDtoB("",self.decimalNumber)
这将调用convertDtoB
,其中decimal
的值为空字符串。这最终会传递给int
并最终评估int('')
,从而导致您遇到错误。
您需要使convertDtoB
对空字符串具有弹性或将调用代码更改为不传递