有谁知道为什么var2.set(E2.get())
函数没有类型而不是int?
var1 = Tkinter.IntVar()
E1 = Tkinter.Entry(root, textvariable = var1 ,bd =5)
E1.pack(side = Tkinter.LEFT)
n = var1.set(E1.get())
我收到了这个错误:
文件" /home/rostom/Desktop/statistic.py",第36行,在estimate_prob中 对于范围内的m(0,m): TypeError:range()期望整数结束参数,得到NoneType。
整个代码
import numpy as np
import Tkinter
import tkMessageBox
root = Tkinter.Tk()
var1 = Tkinter.IntVar()
var2 = Tkinter.IntVar()
var3 = Tkinter.IntVar()
var4 = Tkinter.IntVar()
text1= Tkinter.StringVar()
text2= Tkinter.StringVar()
text3= Tkinter.StringVar()
text4= Tkinter.StringVar()
def seq_sum(n):
x1 = 0
x2 = 0
for i in range(0,n):
r = np.random.rand()
if r > 0.5:
x1=x1+1
else:
x2=x2+1
return x1
def estimate_prob(n,k1,k2,m):
ran=0
for m in range(0,m):
head = seq_sum(n)
if k1<=head < k2:
ran =ran + 1
p = ran/float(m)
print ran
return tkMessageBox.showinfo("results ", p)
L1 = Tkinter.Label(root, textvariable = text1)
L1.grid(row=0,column=0)
E1 = Tkinter.Entry(root, textvariable = var1 ,bd =5)
E1.grid(row=0,column=1)
text1.set("number of flips ")
n = var1.set(E1.get())
L2 = Tkinter.Label(root, textvariable = text2)
L2.grid(row=1,column=0)
E2 = Tkinter.Entry(root, textvariable = var2 ,bd =5)
E2.grid(row=1,column=1)
text2.set("lower limit ")
k1 = var2.set(E2.get())
L3 = Tkinter.Label(root, textvariable = text3)
L3.grid(row=2,column=0)
E3 = Tkinter.Entry(root, textvariable = var3 ,bd =5)
E3.grid(row=2,column=2)
text3.set("upper limit ")
k2= var3.set(E3.get())
L4 = Tkinter.Label(root, textvariable = text4)
L4.grid(row=3,column=0)
E4 = Tkinter.Entry(root, textvariable = var4 ,bd =5)
E4.grid(row=3,column=1)
text4.set("number of trials ")
m = var4.set(E4.get())
B1 = Tkinter.Button(root,text = "calculate the probability", command = lambda: estimate_prob(n,k1,k2,m) )
B1.grid(row=4,column=1)
root.mainloop()
答案 0 :(得分:0)
这句话看起来很奇怪:
ade, 12.3
为什么不把它写成
m = var4.set(E4.get())
因为m = var4.get()
是分配给var4
的IntVar?当您设置 IntVar时,操作将返回E4
。
None
这适用于所有四个IntVars。