Python 小白求助
  • 板块灌水区
  • 楼主New_hope
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/4/3 17:13
  • 上次更新2023/10/28 04:44:09
查看原帖
Python 小白求助
416242
New_hope楼主2022/4/3 17:13
import tkinter as tk
import tkinter.messagebox

#-----购买部分开始-----#
def Market():
    window_buyer = tk.Tk()
    window_buyer.title('交易市场')
    window_buyer.geometry('500x300')

    Market_lab = tk.Label(window_buyer,text='欢迎',bg='green',font=('consolas',20),width=40,height=2).pack()
    
    def Buy_drinks():
        win_drinks = tk.Toplevel(window_buyer)
        win_drinks.title('饮品')
        win_drinks.geometry('500x300')
    
        s1 = '可乐'
        s2 = '雪碧'
         but_cola = tk.Button(win_drinks,text=s1,command=Buy(3,s1)).place(x=120,y=100)
        but_sprite = tk.Button(win_drinks,text=s2,command=Buy(2.5,s2)).place(x=300,y=100)
        
    def Buy_foods():
        win_foods = tk.Toplevel(window_buyer)
        win_foods.title('食品')
        win_foods.geometry('500x300')

        s3 = '汉堡'
        s4 = '薯条'
        but_ham = tk.Button(win_foods,text=s3,command=Buy(10,s3)).place(x=120,y=100)
        but_chip = tk.Button(win_foods,text=s4,command=Buy(5,s4)).place(x=300,y=100)
        
    def Buy_others():
        win_others = tk.Toplevel(window_buyer)
        win_others.title('其他')
        win_others.geometry('500x300')

        s5 = '花朵'
        s6 = '文具'
        but_flo = tk.Button(win_others,text=s5,command=Buy(15,s5)).place(x=120,y=100)
        but_sch = tk.Button(win_others,text=s6,command=Buy(5,s6)).place(x=300,y=100)

    def Buy(price,name):
        win_buy = tk.Tk()
        win_buy.title('Buy'+' '+name)
        win_buy.geometry('400x400')
        
        Price = tk.Label(win_buy,text='一份'+str(price)+'元').pack()

    but_foods = tk.Button(window_buyer,text='食品',command=Buy_foods).place(x=130,y=100)
    but_drinks = tk.Button(window_buyer,text='饮品',command=Buy_drinks).place(x=215,y=100)
    but_others = tk.Button(window_buyer,text='其他',command=Buy_others).place(x=300,y=100)
    
#-----购买部分结束-----#
    
#-----登录部分开始-----#
window = tk.Tk()
window.title('Charity Sale Login System')
window.geometry('450x300')
user_name=''
user_password=''
##登录part

def user_login():
    
    x = var_user_name.get()
    y = var_user_pwd.get()
    
    if user_name == '' or x == '':
        yes_or_no = tk.messagebox.askyesno(title='错误',message='看起来你还没有注册,今天注册吗')
        if yes_or_no:
            user_sign_up()
    elif y != user_password:
        tk.messagebox.showinfo(title='错误',message='看起来你输错了密码')
    elif x == user_name and y == user_password:
        tk.messagebox.showinfo(title='恭喜',message='你成功登录了,好好享受时光吧')
        d = tk.messagebox.askyesno(title='接着',message='你想来市场吗')
        if d :
            Market()
            window.destroy()
        else:
            window.destroy()

##注册part
def user_sign_up():
    

    window_sign_up = tk.Toplevel(window)
    window_sign_up.title('注册窗口')
    window_sign_up.geometry('350x275')
    
    var_new_name=tk.StringVar()
    var_new_name.set('example@Python.com')
    var_new_pwd=tk.StringVar()
    var_confirm_pwd=tk.StringVar()

    tk.Label(window_sign_up,text='用户名:').place(x=7,y=40)
    entry_new_name = tk.Entry(window_sign_up,textvariable=var_new_name).place(x=130,y=40)
    
    tk.Label(window_sign_up,text='新密码:').place(x=7,y=80)
    entry_new_pwd = tk.Entry(window_sign_up,textvariable=var_new_pwd,show='*').place(x=130,y=80)
    
    tk.Label(window_sign_up,text='再次确认:').place(x=7,y=120)
    entry_confirm_pwd = tk.Entry(window_sign_up,textvariable=var_confirm_pwd,show='*').place(x=130,y=120)
    
    def apply():
        
        global user_name,user_password
        
        user_nname = var_new_name.get()
        user_new_pwd = var_new_pwd.get()
        user_confirm_pwd = var_confirm_pwd.get()
        
        ###第二次输入密码时出错
        if user_new_pwd != user_confirm_pwd:
            decision = tk.messagebox.askyesno(title='错误',message='你可能忘记密码了,要再试一遍吗')
            if decision:
                var_new_pwd.set('')
                var_confirm_pwd.set('')
            else:
                window_sign_up.destroy()

        ###使用样例用户名
        elif user_nname == 'example@Python.com':
            decision = tk.messagebox.askyesno(title='错误',message='这不能用,换另一个好名字吧')
            if decision:
                var_new_name.set('example@Python.com')
                var_new_pwd.set('')
                var_confirm_pwd.set('')
            else:
                window_sign_up.destroy()
        
        ###啥都没输入
        elif user_nname == '' or user_new_pwd == '':
            decision = tk.messagebox.askyesno(title='错误',message='你认真的吗')
            if decision:
                pass
            else:
                window_sign_up.destroy()
            
        ###成功注册
        elif user_new_pwd == user_confirm_pwd and user_nname != 'example@Python.com':
            user_name = var_new_name.get()
            user_password = var_new_pwd.get()
            tk.messagebox.showinfo(title='恭喜',message='你成功注册了,你仅仅只需再次登录!')
            window_sign_up.destroy()
            
    
    button_sure = tk.Button(window_sign_up,text='确认',command=apply).place(x=130,y=180)

##标题
l = tk.Label(window,text='爱心义卖系统',bg='Red',font=('consolas',20),width=30,height=2)
l.pack()

##使用者信息
var_user_name=tk.StringVar()
var_user_name.set('example@python.com')
var_user_pwd=tk.StringVar()

tk.Label(window,text='用户名:').place(x=50,y=120)
tk.Label(window,text='密码:').place(x=50,y=160)

entry_user_name = tk.Entry(window,textvariable=var_user_name).place(x=160,y=120)
entry_user_pwd = tk.Entry(window,textvariable=var_user_pwd,show='*').place(x=160,y=160)

but_login = tk.Button(window,text='登录',command=user_login,width=10,height=2).place(x=140,y=200)
but_sign_up = tk.Button(window,text='注册',command=user_sign_up,width=10,height=2).place(x=250,y=200)

#-----登录部分结束-----#



window.mainloop()


我主要想问购买部分,按钮cola,......不是点了之后才会执行弹窗【Buy()函数】的命令吗,为什么按food......会一起弹出来???

2022/4/3 17:13
加载中...