0

I'm new to python. I'm trying to set up a GUI with a main window and a modal window to receive the database credentials. The main window has to be in the background when the modal window appears and the user should not interact with it. Actually the application should terminate in case the user fails to provide a valid username and password. I'm presenting part of the code here. What I get is the main window (the modal window does not appear at all) and I can't do anything but shut the program down - not directly; only through visual studio. '''

import tkinter as tk

class login:

   def checkDbCredentials(self, user, password):
       if (len(user)<4 or len(password)<4):
           return 0
       return 1

   def ok(self):
       us = self.user.get()
       pw = self.password.get()
       if self.checkDbCredentials(us, pw)==0:
           tk.messagebox.showinfo("Error", "Username and Password do not match")
           sys.exit(0)
       self.dic['user']=us
       self.dic['pass']=pw
       self.win2.destroy()


   def cancel(self, event=None):
      self.parent.focus_set()
      self.destroy()

   def widgets(self):
      x = self.win2.winfo_screenwidth()
      y = self.win2.winfo_screenheight()
      xx = int((x-270)/2)
      yy = int((y-85)/2)
      geo = "270x85+" + str(xx) + "+" + str(yy)
      self.win2.geometry(newGeometry=geo)
      self.tempLab= tk.Label(self.win2, text="Username:", pady=5)
      self.tempLab1 = tk.Label(self.win2, text="Password:")
      self.iuser = tk.Entry(self.win2, textvariable = self.user, width=30)
      self.ipass = tk.Entry(self.win2, textvariable = self.password, width=30, show="*")
      self.tempLab.grid(column=0, row=0)
      self.tempLab1.grid(column=0, row=1)
      self.iuser.grid(column=1, row=0)
      self.ipass.grid(column=1, row=1)
      self.bt = tk.Button(self.win2, text="Submit", command=lambda: self.ok())
      self.bt.grid(column=0, row=2, columnspan=2, pady=5)
      self.win2.bind("<Return>", self.ok)
      self.win2.bind("<Escape>", self.cancel)

   def __init__(self, dic, parent):
      self.win2 = tk.Toplevel(parent)
      self.parent=parent
      self.dic = dic
      self.user = tk.StringVar()
      self.password = tk.StringVar()
      self.win2.overrideredirect(1)
      self.widgets()
      self.win2.transient(parent)
      self.win2.grab_set()
      self.win2.protocol("WM_DELETE_WINDOW", self.cancel)

      self.parent.wait_window(self.win2)

And the main class code is the following:

import tkinter as tk
from tkinter import ttk, X, Y, BOTH
from tkinter import messagebox
import sys
import login


class mainWindow:

   def connect(bt, fr):
       notImplementedYet()

   def notImplementedYet():
       msg = tk.messagebox
       msg.showinfo("Warning", "Not Implemented yet!")

   btX = 20
   btY = 5
   btSunken = '#aec2c2' # sunken button color
   btRaised = '#85adad' # normal button color
   rw = 0 # starting row for buttons
   cl = 0 # starting column

   dbCredentials = {"user":"", "pass":""}

   win = tk.Tk()
   win.title("Title")
   win.geometry(newGeometry="1366x700+00+00")
   win.iconbitmap(bitmap="bitmap.ico")

   fr1 = tk.Frame(win, width=1366, height=50, background='beige')
   fr1.grid(column=cl, row=rw, rowspan=5)
   fr2 = tk.Frame(win)
   fr2.grid(column=0, row=1)
   lb2 = tk.Label(fr2, text="frame         2", background=btRaised)
   btConnect = tk.Button(fr1, text="Connect", background = btRaised ,command=lambda: connect(btConnect, fr1), width=btX, height=btY)
   btConnect.grid(column=0, row=0)

   log = login.login(dbCredentials, win)
   win.wait_window(log.win2)
   print(dbCredentials)

   win.mainloop()

win = mainWindow()

The question is why is the modal window not appearing? I've tried placing the 'wait_window' in the other class as well but it's still not working.

pzogr
  • 424
  • 1
  • 12
  • 30

0 Answers0