Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Tkinter button help and general commands
#1
Hey guys, sorry this is gonna be a long thread and I'm very basic at python and tkinter, so just stay with me Big Grin .

So im trying to make a login window, and i have coded this, which makes the window but the button doesn't work and i don't really understand .self command:

import tkinter
window = tkinter.Tk()
window.geometry('250x190')
window.title("Encrypter")

U = tkinter.Label(window, text = "Username", height = 4)
U.grid(row = 1, column = 0, padx =1, pady = 1)
UE = tkinter.Entry(window, width = 20)
UE.grid(row = 1, column = 1, padx = 1, pady = 1)

P = tkinter.Label(window, text = "Password")
P.grid(row = 2, column = 0, padx = 1, pady = 1)
PE = tkinter.Entry(window)
PE.grid(row = 2, column = 1 , padx = 1, pady = 1)

EB = tkinter.Button(window, text = "Validate ID", fg = "white", bg = "red")
EB.grid(row = 3, column = 1 , padx = 1, pady = 1)

CB = tkinter.Checkbutton(window, text = "keep logged in")
CB.grid(columnspan = 2)


def tkinter_button_clicked(self):
        # print("Clicked")
        username = UE.get()
        password = PE.get()

        # print(username, password)

        if username == "iceman" and password == "Password":
            tm.showinfo("Login info", "Welcome Iceman")
        else:
            tm.showerror("Login error", "Incorrect username")

window.mainloop()
I've looked at another person's example and it works but im trying to learn myself, but here's theirs:

from tkinter import *
import tkinter.messagebox as tm


class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)

        self.checkbox = Checkbutton(self, text="Keep me logged in")
        self.checkbox.grid(columnspan=2)

        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2)

        self.pack()

    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)

        if username == "Iceman" and password == "Password":
            tm.showinfo("Login info", "Welcome Iceman")
        else:
            tm.showerror("Login error", "Incorrect username")


root = Tk()
lf = LoginFrame(root)
root.mainloop()
If you made it this far welldone and thanks for any help.
Quote
#2
The button has not had an event bound to it by setting the callback function on the command parameter.
self is not required when using the function outside of a class as its no longer a method.
The called function will need to be moved so it is defined before trying to add it to the command parameter.
tkinter.messagebox will need importing before you can use it.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 242 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 346 May-12-2019, 08:17 PM
Last Post: joe_momma
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 384 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Help with tkinter; images and button commands SheeppOSU 2 410 Mar-28-2019, 02:01 AM
Last Post: woooee
  Tkinter Button Settings kaanertugrul 3 649 Jan-25-2019, 03:41 PM
Last Post: Larz60+
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 5,215 Aug-05-2018, 11:01 PM
Last Post: woooee
  [Tkinter] Tkinter newly created button does not execute command ChipsSlave 7 1,412 Jul-25-2018, 01:01 AM
Last Post: JUANCARLOS
  Tkinter, button doesnt working ;// Dzonzi 3 1,822 Jul-18-2018, 11:14 AM
Last Post: buran
  [Tkinter] loop function when called from tkinter button click WantedStarling 5 1,757 Jul-13-2018, 06:12 PM
Last Post: nilamo
  [Tkinter] Button widget gets stuck from using tkinter.messagebox? Nwb 2 1,022 Jun-20-2018, 02:21 PM
Last Post: Nwb

Forum Jump:


Users browsing this thread: 1 Guest(s)