当我按下radiobutton然后按下提交按钮时,为什么我的代码不会打印任何内容?例如,如果我按下"打开保存" radiobutton,将条目留空并按提交它应打印" test2",但无论我做什么,它都不会打印任何内容。
def Saves():
global saveordelete
saveordelete = 0
global savedname
def openthesave():
saveordelete = 1
def deletethesave():
saveordelete = 2
def opensave():
if saveordelete == 1:
openname = savedname.get() + ".txt"
my_file = Path(openname)
if my_file.is_file():
print("tes1")
else:
print("test2")
elif saveordelete == 2:
openname = savedname.get() + ".txt"
my_file = Path(openname)
if my_file.is_file():
print("test3")
else:
print("test4")
root = Tk()
root.title("Saves")
root.iconbitmap("morseicon.ico")
root.resizable(0,0)
Label(root, text="Name:").grid(row=0, column=0, sticky=W)
savedname = Entry(root, width=20)
savedname.grid(row=0, column=1)
Button(root, text="Submit", width=10, command=opensave, bg="aqua").grid(row=3, column=8)
Label(root, text="Choose kind:").grid(row=1, column=0, sticky=W)
Radiobutton(root, text="Open save", height="1", command=openthesave, value=1).grid(row=2, column=0, sticky=W)
Radiobutton(root, text="Delete save", height="1", command=deletethesave, value=2).grid(row=3, column=0, sticky=W)
root.mainloop()
答案 0 :(得分:1)
saveordelete
是函数openthesave()
和deletethesave()
中的局部变量。它们不会更改saveordelete
中定义的全局Saves()
的值。在两个函数中将其标记为global
,或者(更好)使用类。