我想给一个程序运行前进行先验证激活,激活成功后才能进入程序计算部分。
先创建了一个窗口文件,验证输入的激活码是否与给定的文档中的激活码一致,如果一致就进入计算模块。
计算部分是之前做好的一个有窗口的程序,暂叫Radas.py,在这个激活程序的窗口程序中,希望激活后,这个激活窗口关闭,单独继续运行Radas,因为Radas本身是个有窗口的程序。
我的问题是,目前激活后,激活窗口也始终存在,出现两个窗口并存的情况。
import tkinter as tk
# 激活码验证
def check_code(code):
with open('activation_code.txt', 'r') as f:
codes = f.readlines()
codes = [i.strip() for i in codes]
if code in codes:
return True
else:
return False
def get_code():
code = entry_code.get()
return code
def on_button_click():
code = get_code()
if check_code(code):
print("激活成功")
# 如果激活码验证通过,则执行 Radas.calculate()
calculate() # 调用新函数
mywindow.quit() # 关闭激活窗口
else:
# 如果激活码验证失败,则打印错误消息
print("激活失败,请重新输入激活码")
return False
def calculate(): # 新函数,可以在这里执行 Radas.calculate() 操作
import Radas
Radas.calculate()
# 创建窗口
mywindow = tk.Tk()
mywindow.geometry('500x70+500+250')
mywindow.title('R激活练习')
mywindow['background'] = "#DFF2F1"
tk.Label(mywindow, text='输入激活码').grid(row=1, column=1)
entry_code = tk.Entry(mywindow)
entry_code.grid(row=1, column=2)
button = tk.Button(mywindow, text="确定", command=on_button_click).grid(row=2, column=1)
button = tk.Button(mywindow, text="退出", command=mywindow.quit).grid(row=2, column=2)
mywindow.mainloop()