on a button click, copy whats inside entry() and store it to a variable (tkinter)

Viewed 99

I have two widgets to work with, a text input, and a button, both are created inside a function. What I want to happen is the user types in their name and then clicks the button to submit the answer. What I want the computer to do, is on the button press it will read whats inside the text and the save it to a variable. Once it saves it, it will print it out.

The code below is bad because it runs through the if statement immediately without the consulting of the button press.

There has to be a simpler solution. Also this may not be PEP 8 or whatever please be patient because I'm new.

import tkinter as tk
from tkinter import Tk, Label, Button
import sys
import time
import random
import threading
from tkinter import *

window = tk.Tk()
window.geometry("300x300")

window.title("GUI")

def start_screen():
    reset()
    start = tk.Label(window, text="start of game")
    start.place(x=110,y=20)
    play = Button(window, text= "play", command = start_game)
    play.place(x=110,y=50)
    helper = Button(window, text="help", command = help_screen)
    helper.place(x=110,y=70)

def stuff():
    global t
    t = True
    print(t)
    return t
 
def text_handling():
    global t
    t = False
    reset()#clears the screen
    label = Label(window, text='')
    question1= "what is your name?"
    label.pack()
    print_slow(label, question1, 40)#prints out letters slowly

    #here is the part I'm having problems with
    name = Entry(window)
    name.pack()
    but = Button(window, text="enter", command= stuff)
    
    but.pack()
    print(t)
    if t == True:
        myPlayer.name = name.get()
        print(myPlayer.name)
    

def start_game():
    reset()
    bt = tk.Button(window,text="Enter", bg="orange", command = 
    text_handling)

    bt.place(x=100,y=100)

start_screen()
0 Answers
Related