Python Forum
.get() invoke after a button nested press
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.get() invoke after a button nested press
#3
(Mar-24-2021, 03:54 AM)deanhystad Wrote: Multichoice_button_0 and Multichoice_button_1 are both None. If you want to keep a handle to these widgets you need to split making the button and placing the button into two separate commands. If you don't need the handles, don't pretend you are saving them in a variable.

This is a big problem:
LoginWindow = tkinter.Tk()
This is how you initialize tkinter. It also creates a window and returns the window handle, but it's primary function is to prepare tkinter for use in your program. This is not how you create a window for a dialog, or a second main window. You can only call Tk() once in your program.

Similarly this is a big problem
LoginWindow.mainloop()
A tkinter program only gets to call mainloop() once. mainloop() will run until the program is shut down (quit) or crashes.

If you fix those problems the next problem you encounter is this:
while login_verified == False and run_program == True:
Now that the mainloop() is removed, there is noting preventing this loop from running forever and creating millions of login windows.

There are so many changes required to your code to make it work that I would throw it away and start from scratch. I started by making a login dialog that gets the username and password. It is based off the tkinter.simpledialog.Dialog class. I override the body and buttonbox methods to create my own controls.
import tkinter as tk
from tkinter import simpledialog, messagebox

class LoginDialog(simpledialog.Dialog):
    def __init__(self, parent, title='Login'):
        super().__init__(parent, title)

    def body(self, frame):
        """Places controls in the body of the dialog.  Overrides base method"""
        self.username = None
        self.password = None
        self.cancelled = False

        tk.Label(frame, text="Username: ").grid(row=0, column=0, padx=5, pady=5)
        self.username_entry = tk.StringVar()
        tk.Entry(frame, textvariable=self.username_entry).grid(row=0, column=1, pady=10)
    
        tk.Label(frame, text="Password: ").grid(row=1, column=0)
        self.password_entry = tk.StringVar()
        tk.Entry(frame, textvariable=self.password_entry, show="*") \
            .grid(row=1, column=1)

    def buttonbox(self):
        """Make buttons at the bottom of the dialog.  Overrides base method"""
        tk.Button(self, text="Ok", width="10", command=self.ok_pressed) \
            .pack(side='left', padx=5, pady=5)
        tk.Button(self, text="Cancel", width="10", command=self.cancel_pressed) \
            .pack(side='right', padx=5, pady=5)

    def ok_pressed(self):
        self.username = self.username_entry.get()
        self.password = self.password_entry.get()
        self.destroy()

    def cancel_pressed(self):
        self.cancelled = True
        self.destroy()

def login():
    dialog = LoginDialog(root)
    if dialog.cancelled:
        print('Login cancelled')
    elif dialog.username and dialog.password:
        print('Username', dialog.username, '\nPassword', dialog.password)
    else:
        messagebox.showerror('Login Failed', 'Username and/or Password are Incorrect')


root = tk.Tk()
tk.Button(root, text='Push Me', command=login).pack()
root.mainloop()
You can place the password processing in the login() function. Use tkinter Messagebox windows to display messages about successful or unsuccessful login attempts.
Reply


Messages In This Thread
RE: .get() invoke after a button nested press - by iddon5 - Mar-28-2021, 06:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 6,245 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 15,976 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 9 13,779 Jan-18-2021, 10:21 PM
Last Post: Serafim
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 4,087 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Anytime I press the button, the result is depicted Jionni 2 2,283 Feb-24-2020, 10:08 AM
Last Post: Jionni
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,087 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 6,495 Jun-17-2019, 05:28 AM
Last Post: Yoriz
  [Tkinter] Spawn sub-window with button press malonn 3 5,952 Oct-28-2018, 02:56 PM
Last Post: malonn
  [Tkinter] Updating Label After Button Press malonn 7 5,820 Aug-23-2018, 10:52 PM
Last Post: malonn
  [Tkinter] Problem with changing label text on button press xk2006x 1 5,629 Jun-02-2017, 06:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020