我遇到一个类的变量要在另一类中定义的问题。 我想验证其他类“ SeaofBTCapp”中def“ number1”中类“ ChoseNumbers”的数量。 代码:
import Tkinter as tk
import serial
LARGE_FONT = ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.title("Soft-Starter")
self.geometry('380x320')
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (Home, ChoseNumbers):
frame = F (container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(Home)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def close(self):
#port = "COM3"
#baud = 115200
#ser = serial.Serial(port, baud)
#ser.close()
#if (ser.isOpen() == False):
# print ser.name + " is close."
self.destroy()
def number1(self):
entrada1 = tk.Entry(self)
number1 = float(entrada1.get())
if ((number1 <= 50) & (number1 >= 5)):
return var1.set(number1)
else:
print "Error: Value invalid"
class Home(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(background = '#DCDCDC')
label = tk.Label(self, text = "Menu", bg = '#DCDCDC', font = LARGE_FONT)
label.pack(pady = 10, padx = 10)
button = tk.Button(self, text = "Escolher valores", command = lambda: controller.show_frame(ChoseNumbers))
button.pack(side = "top")
button1 = tk.Button(self, text = "Sair", command = lambda: controller.close())
button1.pack(side = "top")
class ChoseNumbers(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(background = '#DCDCDC')
label = tk.Label(self, text = "Escolher valores", bg = '#DCDCDC', font = LARGE_FONT)
label.pack(pady = 10, padx = 10)
var1 = tk.StringVar()
var2 = tk.StringVar()
e1 = tk.Label(self, text = "Tempo de subida", bg = 'gray', fg = "white")
e1.pack(padx = 5, pady = 5, ipadx = 5, ipady = 5, fill = tk.X)
entrada1 = tk.Entry(self)
entrada1.pack(fill = tk.X, padx = 5, pady = 5, ipadx = 5, ipady = 5)
buttonCheck1 = tk.Button(self, text = "Verificar numero", fg = "black", command = lambda: controller.number1())
buttonCheck1.pack(side = "top")
res1 = tk.Label(self, bg = "azure4", fg = "white", textvariable = var1, padx = 5, pady = 5, widt = 3)
res1.pack()
button2 = tk.Button(self, text = "Menu", command = lambda: controller.show_frame(Home))
button2.pack()
button3 = tk.Button(self, text = "Sair", command = lambda: controller.close())
button3.pack(side = "top")
app = SeaofBTCapp()
app.mainloop()
现在,错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1537, in __call__
return self.func(*args)
File "C:\Users\usuario\Desktop\screnn-soft\teste_screnn_soft.py", line 86, in <lambda>
buttonCheck1 = tk.Button(self, text = "Verificar numero", fg = "black", command = lambda: controller.number1())
File "C:\Users\usuario\Desktop\screnn-soft\teste_screnn_soft.py", line 48, in number1
number1 = float(entrada1.get())
ValueError: could not convert string to float:
我正在使用tkinter为学校做一些布局。我还不太熟悉python。请,如果您知道如何解决此问题,请回答,非常感谢!
答案 0 :(得分:0)
该错误消息表明entrada.get()
无法将float(entrada.get())
的结果解析为浮点数。由于这是用户输入字段,因此需要确保您的用户仅在此字段中输入有效的浮动。