以下两题各选一题用Python语言并编程实现其功能 简易计算器,窗口界面自由发挥,编程实现两个整数的加、减、乘、除操作。要求如下: 首先进行操作数文本框检验,检验输入数据是否为纯数字; 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。 用户登录窗口,主窗口界面如图1、图2所示,编程实现用户注册和登录功能 (1)用户注册功能要求如下: 输入时要进行用户名、密码校验,规则为用户名不为空且必须包含数字和字母,即以数字或者字母开头,后面可以带数字、字母或者_;用户密码不为空且仅包含数字和字母。 当用户名不重复时,用户名、密码信息保存在当前文件夹下的文件user.txt中,并弹出消息窗口提示“注册成功!”;否则,弹出消息窗口提示“该用户名已存在,请重新输入”。  (2)用户登录功能要求如下: 输入时要进行用户名、密码校验,当用户名、密码与文件user.txt中某一组用户账号(即用户名、密码)对应时,并弹出消息窗口提示“登录成功!”,关闭该消息窗口后再弹出子窗口;否则,弹出消息窗口提示“用户名或者密码错误”。 
时间: 2024-02-15 17:05:12 浏览: 64
简易计算器代码如下:
```python
import tkinter as tk
def calculate():
num1 = entry1.get()
num2 = entry2.get()
if not num1.isdigit() or not num2.isdigit():
result.set("请输入整数!")
return
num1 = int(num1)
num2 = int(num2)
operator = operator_var.get()
if operator == "+":
result.set(num1 + num2)
elif operator == "-":
result.set(num1 - num2)
elif operator == "*":
result.set(num1 * num2)
elif operator == "/":
if num2 == 0:
result.set("除数不能为0!")
else:
result.set(num1 / num2)
root = tk.Tk()
root.title("简易计算器")
# 第一行:输入框和标签
entry1 = tk.Entry(root, width=10)
entry1.grid(row=0, column=0, padx=5, pady=5)
label1 = tk.Label(root, text="操作数1")
label1.grid(row=0, column=1, padx=5, pady=5)
entry2 = tk.Entry(root, width=10)
entry2.grid(row=0, column=2, padx=5, pady=5)
label2 = tk.Label(root, text="操作数2")
label2.grid(row=0, column=3, padx=5, pady=5)
# 第二行:加减乘除单选框
operator_var = tk.StringVar()
operator_var.set("+")
add_radiobutton = tk.Radiobutton(root, text="+", variable=operator_var, value="+")
add_radiobutton.grid(row=1, column=0, padx=5, pady=5)
sub_radiobutton = tk.Radiobutton(root, text="-", variable=operator_var, value="-")
sub_radiobutton.grid(row=1, column=1, padx=5, pady=5)
mul_radiobutton = tk.Radiobutton(root, text="*", variable=operator_var, value="*")
mul_radiobutton.grid(row=1, column=2, padx=5, pady=5)
div_radiobutton = tk.Radiobutton(root, text="/", variable=operator_var, value="/")
div_radiobutton.grid(row=1, column=3, padx=5, pady=5)
# 第三行:计算按钮和结果显示
button = tk.Button(root, text="计算", command=calculate)
button.grid(row=2, column=0, padx=5, pady=5)
result = tk.StringVar()
result.set("结果")
result_label = tk.Label(root, textvariable=result)
result_label.grid(row=2, column=1, columnspan=3, padx=5, pady=5)
root.mainloop()
```
用户登录窗口代码如下:
```python
import tkinter as tk
import re
def register():
username = username_entry.get()
password = password_entry.get()
if not re.match("^[a-zA-Z\d][a-zA-Z\d_]*$", username):
register_result.set("用户名不符合规则!")
return
if not re.match("^[a-zA-Z\d]+$", password):
register_result.set("密码不符合规则!")
return
with open("user.txt", "r") as f:
for line in f:
if username == line.split()[0]:
register_result.set("该用户名已存在,请重新输入!")
return
with open("user.txt", "a") as f:
f.write("{} {}\n".format(username, password))
register_result.set("注册成功!")
def login():
username = username_entry.get()
password = password_entry.get()
with open("user.txt", "r") as f:
for line in f:
if username == line.split()[0] and password == line.split()[1]:
login_result.set("登录成功!")
subwindow = tk.Toplevel(root)
subwindow.title("子窗口")
subwindow.geometry("200x100")
sub_label = tk.Label(subwindow, text="这是一个子窗口!")
sub_label.pack()
return
login_result.set("用户名或者密码错误!")
root = tk.Tk()
root.title("用户登录窗口")
# 第一行:用户名输入框和标签
username_entry = tk.Entry(root, width=20)
username_entry.grid(row=0, column=0, padx=5, pady=5)
username_label = tk.Label(root, text="用户名")
username_label.grid(row=0, column=1, padx=5, pady=5)
# 第二行:密码输入框和标签
password_entry = tk.Entry(root, width=20, show="*")
password_entry.grid(row=1, column=0, padx=5, pady=5)
password_label = tk.Label(root, text="密码")
password_label.grid(row=1, column=1, padx=5, pady=5)
# 第三行:注册和登录按钮
register_button = tk.Button(root, text="注册", command=register)
register_button.grid(row=2, column=0, padx=5, pady=5)
login_button = tk.Button(root, text="登录", command=login)
login_button.grid(row=2, column=1, padx=5, pady=5)
# 第四行:注册和登录结果显示
register_result = tk.StringVar()
register_result.set("注册结果")
register_label = tk.Label(root, textvariable=register_result)
register_label.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
login_result = tk.StringVar()
login_result.set("登录结果")
login_label = tk.Label(root, textvariable=login_result)
login_label.grid(row=4, column=0, columnspan=2, padx=5, pady=5)
root.mainloop()
```
阅读全文