-1

So! I take an input into an entry box in tkinter, and when the user clicks submit, I want a function to run. Example of my code is below - error is that 'Login is not defined'. How can I fix this? Where should I be defining my function?

import tkinter as tk
from tkinter import ttk

class Window(tk.Tk):
    

    def __init__(self):
        
        super().__init__()

        self.geometry('1000x500+120+250')

   
        #creates entry button
        entry_button = ttk.Button(self, text = "Enter", command = Login)
        entry_button.pack()
        
        #creates username entry box
        userName_entry = ttk.Entry(self, textvariable = userName)
        userName_entry.pack()


openingWin = Window()
openingWin.mainloop()

 def Login:
    userName = userName_entry.get()
    userName_entry.config(state = "disabled")
    print(userName)
  • 3
    Please share a minimum reproducible example that show what you are trying to do. https://stackoverflow.com/help/minimal-reproducible-example – Gonzalo Odiard Sep 27 '22 at 21:01
  • 1
    You don't need to paste your actual code. Create a completely new program that has a class with nothing more than an entry, a button, and a function that ties them together. Try to get that example to work, and then take what you learned to fix your program. – Bryan Oakley Sep 27 '22 at 21:26
  • Please tell us the error. if the program dosnt throw please explain so – william Sep 27 '22 at 22:23
  • Don't use keyword input. – toyota Supra Sep 28 '22 at 01:19
  • The error is `getUsername is not defined`, and looking at the code it seems pretty clear that that's the problem. You haven't defined `getUsername` anywhere. However, the code you posted doesn't actually give that error. It has several other problems. – Bryan Oakley Sep 28 '22 at 22:59
  • Can u post snippet getUsername method? – toyota Supra Sep 29 '22 at 08:54

2 Answers2

0

this is an example of tkinter buttons

import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root)
root.title("menu")
frame.pack()
closemenubutton = tk.Button(frame,
                                text="close menu",
                                command=root.destroy)
closemenubutton.pack(side=tk.RIGHT)
root.mainloop()

if you need to run something else while its running do this:

import threading
import time
import tkinter as tk
def demo():
    root = tk.Tk()
    frame = tk.Frame(root)
    root.title("menu")
    frame.pack()
    closemenubutton = tk.Button(frame,
                                    text="close menu",
                                    command=root.destroy)
    closemenubutton.pack(side=tk.RIGHT)
    root.mainloop()
threading.Thread(target=demo).start()
while True:
    time.sleep(3)
    print("replace this line and the two above with the rest of your code")

The button function creates a button. You have 3 main parameters the frame parameter is just the frame. The text parameter is what the button will say. The command parameter is assigned a function. When your button is pressed it should run the function .pack() inserts the button into the gui

william
  • 55
  • 7
  • Thank you so much - I have updated my question to hopefully be a little clearer and have some sample code, if you'd like to have another look! – beccaroonie Sep 28 '22 at 19:41
0

give this a try

import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root)


def do_something_with_usr_name(name):
    print(name)
    # ^ replace this with your code. name should be a string containing the username


# create the window
root.title("menu")
frame.pack()

# Make a label telling the user what to do
howto = tk.Label(
    root,
    text="put username below then press button")

# create a writeable box for the name
box_input = tk.Text(root,
                    height=1,
                    width=49)

# create the button that gets the name and runs function
output = tk.Button(root,
                   text="button",
                   command=lambda: do_something_with_usr_name(str(box_input.get("1.0", 'end-1c'))))
#     allows func parameters ^     run the function ^          ^  get the text inside the box   ^

# insert them all into the frame
howto.pack(side=tk.TOP)
box_input.pack(side=tk.TOP)
output.pack(side=tk.BOTTOM)

root.mainloop()
william
  • 55
  • 7
  • Where is getUsername is not in or code? – toyota Supra Sep 29 '22 at 08:52
  • when you press the button `output` it runs a function `do_something_with_usr_name()`. this function does something with a string input (in this case your username) the `str(box_input.get("1.0", 'end-1c')))` gets the text of the box `box_input` then sets it as the `name` parameter – william Sep 29 '22 at 19:55