使用tkinter编写界面

# *-* coding:utf8 *-*
import tkinter as tk
import tkinter.messagebox
  
window = tk.Tk()  # 实例化Tk
window.title("login")  # 设置标题
window.geometry("450x300")  # 设置窗口的大小
user_info = [{"name": "admin", "pwd": "1"}, {"name": "user", "pwd": "1"}]  # 存储账号信息
# 定义函数
def login():
    user_info_name = [i["name"] for i in user_info]
    user_info_pwd = [i["pwd"] for i in user_info]
    user_name = var_user_name.get()
    user_pwd = var_pwd.get()
    if user_name in user_info_name:
        if user_pwd == user_info_pwd[user_info_name.index(user_name)]:
            tk.messagebox.showinfo(title="欢迎回来", message="How are you")
        else:
            tk.messagebox.showerror(message="pwd error")
    else:
        answer = tk.messagebox.askquestion(title="warn", message="您还没有账号,是否需要注册?")
        if answer:
            sign()
def sign():
    def store():
        new_pwd1 = new_pwd.get()
        new_user_name1 = new_user_name.get()
        user_info.append({"name": new_user_name1, "pwd": new_pwd1})
        tk.messagebox.showinfo(message="注册成功")
        window_sign.destroy()  # 关闭窗口
    def close():
        window_sign.destroy()
    window_sign = tk.Toplevel(window)  # 窗口上的窗口
    window_sign.geometry("350x200")
    window_sign.title("Sign up window")
    # user
    tk.Label(window_sign, text="Name:").place(x=60, y=60)
    new_user_name = tk.StringVar()  # 注意要使用.get()得到值
    new_user_name.set("exampe@python.com")  # 默认值
    tk.Entry(window_sign, textvariable=new_user_name).place(x=90, y=60)  # 输入用户名
    # pwd
    tk.Label(window_sign, text="Pwd:").place(x=60, y=100)
    new_pwd = tk.StringVar()
    tk.Entry(window_sign, textvariable=new_pwd, show="*").place(x=90, y=100)  # 输入密码
    # 确认按钮
    tk.Button(window_sign, text="确认", command=store).place(x=100, y=140)
    # 取消按钮
    tk.Button(window_sign, text="取消", command=close).place(x=170, y=140)
if __name__ == '__main__':
     
    tk.Label(window, text="User Name:").place(x=50, y=50)
    var_user_name = tk.StringVar()  # 注意要使用.get()得到值
    var_user_name.set("exampe@python.com")  # 默认值
    tk.Entry(window, textvariable=var_user_name).place(x=130, y=50)  # 输入用户名
    tk.Label(window, text="Password:").place(x=50, y=90)
    var_pwd = tk.StringVar()
    tk.Entry(window, textvariable=var_pwd, show="*").place(x=130, y=90)  # 输入密码
    # login Button
    tk.Button(window, text="Login", command=login).place(x=130, y=130)  # 登录按钮
    # sign Button
    tk.Button(window, text="Sign Up", command=sign).place(x=200, y=130)  # 注册按钮
    window.mainloop()  # 显示窗口

以下是一个简单的Python GUI计算器,使用Tkinter模块。 ```python # 导入Tkinter模块 from tkinter import * # 创建主窗口 root = Tk() root.title("计算器") # 设置计算器的输入框和输出框 input_box = Entry(root, width=40, borderwidth=5) input_box.grid(row=0, column=0, columnspan=4, padx=10, pady=10) output_box = Entry(root, width=40, borderwidth=5) output_box.grid(row=1, column=0, columnspan=4, padx=10, pady=10) # 定义计算函数 def calculate(): # 获取输入框的内容 equation = input_box.get() try: # 尝试计算结果 result = eval(equation) # 将结果显示在输出框中 output_box.delete(0, END) output_box.insert(0, result) except: # 如果计算出错,显示错误信息 output_box.delete(0, END) output_box.insert(0, "Error") # 定义按钮的点击事件 def button_click(number): current = input_box.get() input_box.delete(0, END) input_box.insert(0, str(current) + str(number)) def button_clear(): input_box.delete(0, END) output_box.delete(0, END) def button_add(): current = input_box.get() input_box.delete(0, END) input_box.insert(0, str(current) + "+") def button_subtract(): current = input_box.get() input_box.delete(0, END) input_box.insert(0, str(current) + "-") def button_multiply(): current = input_box.get() input_box.delete(0, END) input_box.insert(0, str(current) + "*") def button_divide(): current = input_box.get() input_box.delete(0, END) input_box.insert(0, str(current) + "/") # 创建按钮 button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1)) button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2)) button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3)) button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4)) button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5)) button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6)) button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7)) button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8)) button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9)) button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0)) button_add = Button(root, text="+", padx=39, pady=20, command=button_add) button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract) button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply) button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide) button_clear = Button(root, text="清空", padx=79, pady=20, command=button_clear) button_equal = Button(root, text="=", padx=91, pady=20, command=calculate) # 显示按钮 button_1.grid(row=2, column=0) button_2.grid(row=2, column=1) button_3.grid(row=2, column=2) button_4.grid(row=3, column=0) button_5.grid(row=3, column=1) button_6.grid(row=3, column=2) button_7.grid(row=4, column=0) button_8.grid(row=4, column=1) button_9.grid(row=4, column=2) button_0.grid(row=5, column=0) button_add.grid(row=5, column=1) button_subtract.grid(row=6, column=0) button_multiply.grid(row=6, column=1) button_divide.grid(row=6, column=2) button_clear.grid(row=7, column=0, columnspan=2) button_equal.grid(row=7, column=2, columnspan=2) # 运行主循环 root.mainloop() ``` 这是一个简单的计算器,支持加减乘除四则运算。用户可以点击按钮输入数字和操作符,计算结果将显示在输出框中。如果计算出错,输出框将显示“Error”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值