CSDNWV
2021-01-16 23:42
采纳率: 100%
浏览 163

tkinter窗口不能够嵌套吗?为什么导入第二个tkinter窗口后点击按钮会无效

  • 第一段是验证码(vcode.py)
  • import string,random
    vcode=''
    words=''.join((string.ascii_letters,string.digits))
    for i in range(6):
        vcode+=random.choice(words)
  •  第二段是登陆界面框架
    • import tkinter as tk
      import vcode
      
      window=tk.Tk()
      window.title("PY系统登陆界面")
      sw=window.winfo_screenwidth()
      sh=window.winfo_screenheight()
      w=310;h=160
      x=(sw-w)/2;y=(sh-h)/2
      window.geometry("%dx%d+%d+%d"%(w,h,x,y))
      
      tk.Label(window,text='用户账号:').grid(row=0,column=0)
      tk.Label(window,text='用户密码:').grid(row=1,column=0)
      tk.Label(window,text='验 证 码:').grid(row=2,column=0)
      tk.Label(window,text='%s'%vcode.vcode).grid(row=3,column=1)
      
      usr=tk.StringVar();pwd=tk.StringVar();vcode=tk.StringVar()
      
      e1=tk.Entry(window,textvariable=usr).grid(row=0,column=1,pady=5)
      e2=tk.Entry(window,textvariable=pwd,show='*').grid(row=1,column=1,pady=5)
      e3=tk.Entry(window,textvariable=vcode).grid(row=2,column=1,pady=5)
      
      def tip():
          print("用户账号:",usr.get())
          print("用户密码:",pwd.get())
      def login():
          import login
          
      loginbt=tk.Button(window,text='注册',width=12,command=login).grid(row=4,column=0,sticky=tk.W,padx=10,pady=10)
      logonbt=tk.Button(window,text='登录',width=12,command=window.destroy).grid(row=4,column=1,sticky=tk.E,padx=10,pady=10)
      
      tk.mainloop()
      
      ​

      第三段(login.py,在第二段中导入的正是这一段)原目的是想加入一个注册的窗口,但是导入后点击注册或者清除都没有反应

    • import tkinter as tk
      
      logwindow=tk.Tk()
      logwindow.title("PY系统账号注册")
      sw=logwindow.winfo_screenwidth()
      sh=logwindow.winfo_screenheight()
      w=310;h=115
      x=(sw-w)/2;y=(sh-h)/2
      logwindow.geometry("%dx%d+%d+%d"%(w,h,x,y))
      
      tk.Label(logwindow,text='用户账号:').grid(row=0,column=0)
      tk.Label(logwindow,text='用户密码:').grid(row=1,column=0)
      
      logusr=tk.StringVar();logpwd=tk.StringVar();
      
      logusre=tk.Entry(logwindow,textvariable=logusr).grid(row=0,column=1,padx=10,pady=5)
      logpwde=tk.Entry(logwindow,textvariable=logpwd).grid(row=1,column=1,padx=10,pady=5)
      
      def tip():
          print("用户账号:",logusr.get())
          print("用户密码:",logpwd.get())
      def cancel():
          logusr.set('')
          logpwd.set('')
      
      tk.Button(logwindow,text='注册',width=12,command=tip).grid(row=3,column=0,sticky=tk.W,padx=10,pady=10)
      tk.Button(logwindow,text='清除',width=12,command=cancel).grid(row=3,column=1,sticky=tk.E,padx=10,pady=10)
      
      tk.mainloop()
      

      (自学阶段实在很多不理解,可轻喷,请各路大神不吝赐教)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • coagenth 2021-01-19 00:02
    已采纳

    在你的代码基础上进行修改,结合sqlite3库,将新注册用户存入数据库,根据用户输入情况进行校验。完整代码:

    import random
    import string
    import tkinter as tk
    import sqlite3
    from tkinter.messagebox import showinfo, showwarning, showerror
    window = tk.Tk()
    window.title("PY系统登陆界面")
    sw = window.winfo_screenwidth()
    sh = window.winfo_screenheight()
    w = 310
    h = 160
    x = (sw-w)/2
    y = (sh-h)/2
    window.geometry("%dx%d+%d+%d" % (w, h, x, y))
    
    vcode = ''
    words = ''.join((string.ascii_letters, string.digits))
    for i in range(6):
        vcode += random.choice(words)
    
    tk.Label(window, text='用户账号:').grid(row=0, column=0)
    tk.Label(window, text='用户密码:').grid(row=1, column=0)
    tk.Label(window, text='验 证 码:').grid(row=2, column=0)
    tk.Label(window, text='%s' % vcode).grid(row=3, column=1)
    
    usr = tk.StringVar()
    pwd = tk.StringVar()
    v_code = tk.StringVar()
    
    e1 = tk.Entry(window, textvariable=usr).grid(row=0, column=1, pady=5)
    e2 = tk.Entry(window, textvariable=pwd, show='*').grid(row=1, column=1, pady=5)
    e3 = tk.Entry(window, textvariable=v_code).grid(row=2, column=1, pady=5)
    
    
    def check():
        name = usr.get()
        psw=pwd.get()
        check=v_code.get()
        print("用户账号:", name)
        print("用户密码:",psw )
        conn = sqlite3.connect('users.db')
        cur = conn.cursor()
        sq='select name from users;'
        sq1 = 'select password from users;'
        if name not in [x[0] for x in cur.execute(sq)] or psw not in [x[0] for x in cur.execute(sq1)] or check!=vcode:
            showerror(title='Error',message='Invalid username or password or checkcode! Please input again!')
        else:
            showinfo(title='OK',
                      message=f'Welcome {name}!')
    
    
    def login():
        logwindow = tk.Tk()
    
    
        logwindow.title("PY系统账号注册")
        sw = logwindow.winfo_screenwidth()
        sh = logwindow.winfo_screenheight()
        w = 310
        h = 115
        x = (sw-w)/2
        y = (sh-h)/2
        logwindow.geometry("%dx%d+%d+%d" % (w, h, x, y))
    
        tk.Label(logwindow, text='用户账号:').grid(row=0, column=0)
        tk.Label(logwindow, text='用户密码:').grid(row=1, column=0)
    
        logusr = tk.StringVar()
        logpwd = tk.StringVar()
    
        logusre = tk.Entry(logwindow, textvariable=logusr)
        logusre.grid(
            row=0, column=1, padx=10, pady=5)
        logpwde = tk.Entry(logwindow, textvariable=logpwd)
        logpwde.grid(
            row=1, column=1, padx=10, pady=5)
    
        def tip():
            
            print("用户账号:", logusre.get())
            print("用户密码:", logpwde.get())
            conn=sqlite3.connect('users.db')
            cur=conn.cursor()
            #cur.execute('drop table if exists users;')
            
            cur.execute(
                'create table if not exists users(id integer autoincreament,name text,password text);')
            s2='insert into users(name,password) values(?,?);'
            cur.execute(s2, [logusre.get(), logpwde.get()])
            
            conn.commit()
        
        tk.Button(logwindow, text='注册', width=12, command=tip).grid(
            row=3, column=0, sticky=tk.W, padx=10, pady=10)
        tk.Button(logwindow, text='退出', width=12, command=logwindow.destroy).grid(
            row=3, column=1, sticky=tk.E, padx=10, pady=10)
    
        logwindow.mainloop()
    
    
    loginbt = tk.Button(window, text='注册', width=10, command=login).grid(
        row=4, column=0, sticky=tk.W, padx=8, pady=10)
    logonbt = tk.Button(window, text='登录', width=10, command=check).grid(
        row=4, column=1, sticky=tk.E, padx=8, pady=10)
    logonbt = tk.Button(window, text='退出', width=10, command=window.destroy).grid(
        row=4, column=2, sticky=tk.E, padx=8, pady=10)
    window.mainloop()
    2 打赏 评论
  • RealUncleMark 2021-01-17 07:27

    你关注我后,订阅一个专栏, 内有tkinter字样,然后我们可以讨论。

    打赏 评论

相关推荐 更多相似问题