def sign_in_f(): user_si_name = name_entry.get() user_si_pass = password_entry.get() if user_si_name in success: i = success.index(user_si_name) if success[i+1] == user_si_pass: import sys def sign_in_f(): user_si_name = name_entry.get() user_si_pass = password_entry.get() if user_si_name in success: i = success.index(user_si_name) if success[i + 1] == user_si_pass: with open('anjian.py', 'r', encoding='utf-8') as f: code = compile(f.read(), 'anjian.py', 'exec') exec(code) sys.exit()更改这段代码使界面结束并且使用with打开的程序
时间: 2023-12-12 11:02:24 浏览: 131
Python中if __name__ == '__main__'作用解析
以下是修改后的代码:
```python
import sys
def sign_in_f():
user_si_name = name_entry.get()
user_si_pass = password_entry.get()
if user_si_name in success:
i = success.index(user_si_name)
if success[i+1] == user_si_pass:
with open('anjian.py', 'r', encoding='utf-8') as f:
code = compile(f.read(), 'anjian.py', 'exec')
exec(code)
sys.exit(0)
```
这个修改后的代码在执行完打开程序后,使用 `sys.exit(0)` 来结束界面程序。使用 `with` 语句来打开程序文件,可以确保文件在使用完后会被正确关闭。
阅读全文