我写了一个函数,它将有多达72个IF语句,我希望编写的代码会更短,但不知道从哪里开始
当选择单选按钮时,该函数会读取self.timeselect变量,并将结果保存到名为missing_time.txt的文本文件中.如果结果等于1,则将"0000"保存到文件中,如果结果为2,则将0020保存到文本文件等.这可以是72种可能的组合.
有没有更简单的方法来简化功能?
def buttonaction():
selectedchoice = ""
if self.timeselect.get() == 1:
selectedchoice = "0000"
orig_stdout = sys.stdout
f = open('missing_time.txt', 'w')
sys.stdout = f
print(selectedchoice)
f.close()
if self.timeselect.get() == 2:
selectedchoice = "0020"
orig_stdout = sys.stdout
f = open('missing_time.txt', 'w')
sys.stdout = f
print(selectedchoice)
f.close()
self.timeselect = tkinter.IntVar()
self.Radio_1 = tkinter.Radiobutton(text="0000",variable =
self.timeselect,indicator = 0 ,value=1)
self.Radio_1.place(x=50,y=200)
self.Radio_2 = tkinter.Radiobutton(text="0020",variable =
self.timeselect,indicator = 0 ,value=2)
self.Radio_2.place(x=90,y=200)
Run Code Online (Sandbox Code Playgroud) python-3.x ×1