I set four RadioButtons for users to choose the type of distribution, then use a global variable s_val to record the type of distribution. However, no matter which button users choose, s_val always equals to 0. I wonder what causes this problem.
Here is my code:
`import tkinter as tk
import tkinter.messagebox as messagebox
from numpy import*
#设计交互页面
#s_val强度分布种类
#r_val应力分布种类
para1_1 = 0
para1_2 = 0
para2_1 = 0
para2_2 = 0
class basedesk:
def __init__(self, master):
self.root = master
self.root.config()
self.root.title('可靠性计算')
self.root.geometry('400x300')
initface1(self.root)
class initface1:
def get_val1(self):
global s_val
s_val = self.s_value.get()
def __init__(self, master):
self.master = master
#基准界面initface1
self.initface1 = tk.Frame(self.master, )
self.initface1.pack()
reminder = tk.Label(self.initface1, text='please choose the distribution type of strength', width=50, height=10)
reminder.pack()
#获取应力分布的按键设计
self.s_value = tk.IntVar()
self.radio()
tk.Button(self.initface1, text='确定', command=self.change).pack()
def radio(self):
radio_bt1=tk.Radiobutton(self.initface1, text='normal distribution', variable=self.s_value, value=1,
command=self.get_val1())
radio_bt2=tk.Radiobutton(self.initface1, text='lg normal distribution', variable=self.s_value, value=2,
command=self.get_val1())
radio_bt3=tk.Radiobutton(self.initface1, text='exponential distribution', variable=self.s_value, value=3,
command=self.get_val1())
radio_bt4=tk.Radiobutton(self.initface1, text='weibull distribution', variable=self.s_value, value=4,
command=self.get_val1())
radio_bt1.pack()
radio_bt2.pack()
radio_bt3.pack()
radio_bt4.pack()
def change(self, ):
self.initface1.destroy()
face1(self.master)
class face1:
def __init__(self, master):
self.master = master
self.face1 = tk.Frame(self.master, )
self.face1.pack()
global s_val
print(s_val)
if s_val==1|s_val==2:
tk.Label(self.face1, text='please fill two parameters of this distribution', width=50, height=10).pack()
tk.Label(self.face1, text='mean').pack()
self.parameter1 = tk.Entry(self.face1, relief='groove', width=20)
self.parameter1.pack()
tk.Label(self.face1, text='standard deviation').pack()
self.parameter2 = tk.Entry(self.face1, relief='groove', width=20)
self.parameter2.pack()
else:
tk.Label(self.face1, text='please fill the parameter of this distribution', width=50, height=10).pack()
self.parameter1 = tk.Entry(self.face1, relief='groove', width=20)
self.parameter1.pack()
tk.Label(self.face1, text='caution: only numbers are allowed').pack()
tk.Button(self.face1, text='yes', command=self.com).pack()
def com(self):
global para1_1
global para1_2
global s_val
if s_val == 1 | s_val == 2:
if self.parameter1.get().isdigit()&self.parameter2.get().isdigit():
para1_1 = float(self.parameter1.get())
para1_2 = float(self.parameter2.get())
self.change()
else:
messagebox.showwarning('warning', 'not numbers')
else:
if self.parameter1.get().isdigit():
para1_1 = self.parameter1.get()
para1_2 = 0
self.change()
else:
messagebox.showwarning('warning', 'not numbers')
def change(self,):
self.face1.destroy()
initface2(self.master)
class initface2:
def __init__(self, master):
self.master = master
#第二个确认分布界面
self.initface2 = tk.Frame(self.master, )
self.initface2.pack()
reminder = tk.Label(self.initface2, text='please choose the distribution type of r', width=50, height=10)
reminder.pack()
#获取应力分布的按键设计
self.r_value = tk.IntVar()
radio_bt1 = tk.Radiobutton(self.initface2, text='normal distribution', variable=self.r_value, value=1,
command=self.get_val2())
radio_bt2 = tk.Radiobutton(self.initface2, text='lg normal distribution', variable=self.r_value, value=2,
command=self.get_val2())
radio_bt3 = tk.Radiobutton(self.initface2, text='exponential distribution', variable=self.r_value, value=3,
command=self.get_val2())
radio_bt4 = tk.Radiobutton(self.initface2, text='weibull distribution', variable=self.r_value, value=4,
command=self.get_val2())
radio_bt1.pack()
radio_bt2.pack()
radio_bt3.pack()
radio_bt4.pack()
btn = tk.Button(self.initface2, text='yes', command=self.change)
btn.pack()
def change(self, ):
self.initface2.destroy()
face2(self.master)
def get_val2(self):
global r_val
r_val = int(self.r_value.get())
class face2:
def __init__(self, master):
self.master = master
self.face2 = tk.Frame(self.master, )
self.face2.pack()
global r_val
if r_val == 1 | r_val == 2:
tk.Label(self.face2, text='please fill two parameters of this distribution', width=50, height=10).pack()
tk.Label(self.face2, text='mean').pack()
self.parameter3 = tk.Entry(self.face2, relief='groove', width=20)
self.parameter3.pack()
tk.Label(self.face2, text='standard deviation').pack()
self.parameter4 = tk.Entry(self.face2, relief='groove', width=20)
self.parameter4.pack()
else:
tk.Label(self.face2, text='please fill the parameter of this distribution', width=50, height=10).pack()
self.parameter3 = tk.Entry(self.face2, relief='groove', width=20)
self.parameter3.pack()
tk.Label(self.face2, text='caution: only numbers are allowed').pack()
tk.Button(self.face2, text='yes', command=self.com).pack()
def com(self):
global para2_2
global para2_1
global r_val
if r_val == 1 | r_val == 2:
if self.parameter3.get().isdigit() & self.parameter4.get().isdigit():
para2_1 = float(self.parameter3.get())
para2_2 = float(self.parameter4.get())
else:
messagebox.showwarning('warning', 'not numbers')
else:
if self.parameter3.get().isdigit():
para2_1 = float(self.parameter3.get())
para2_2 = 0
else:
messagebox.showwarning('warning', 'not numbers')
self.cal_res(s_val, r_val, para1_1, para1_2, para2_1, para2_2)
def cal_res(self,type1, type2, p1, p2, p3, p4):
if type1==1:
x1 = random.normal(p1, p2, size=(1000, 1))
elif type1==2:
x1 = random.lognormal(p1, p2, size=(1000, 1)), 2
elif type1==3:
x1 = random.weibull(p1, size=(1000, 1)), 2
else:
x1 = random.exponential(float(p1), size=(1000, 1)), 2
if type2==1:
x2 = random.normal(p3, p4, size=(1000, 1)), 2
elif type1==2:
x2 = random.lognormal(p3, p4, size=(1000, 1)), 2
elif type1==3:
x2 = random.weibull(p3, size=(1000, 1)), 2
else:
x2 = random.exponential(p3, size=(1000, 1)), 2
times = 0
n1 = 0
while times<1000:
if x1[times] > x2[times]:
n1 = n1+1
times = times+1
result = n1/1000
messagebox.showinfo('result', 'the result is'+str(result))
if __name__ == '__main__':
root = tk.Tk()
basedesk(root)
root.mainloop()
I tried to change the type of s_val, from global variabale to class varibale. Yet it did't work.