Python Forum

Full Version: Clicking on the button crashes the TK window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import time
from tkinter import *

import pyautogui

root = Tk()
root.geometry('400x200')

def button_command():
    text = int (entry1.get())

    time.sleep(3)

    pyautogui.keyDown("w")
    time.sleep(text)
    pyautogui.keyUp("w")

    return None

entry1 = Entry(root, width= 20)
entry1.pack()

button = Button(root, text='Button', command=lambda: button_command())
button.pack()

root.mainloop()
It does not crash for me. I tested this:
import time
import tkinter as tk
import pyautogui
 
root = tk.Tk()
def button_command():
    pyautogui.keyDown("w")
    time.sleep(int(entry.get()))
    pyautogui.keyUp("w")
 
entry = tk.Entry(root, width= 20)
entry.pack()
tk.Button(root, text='Button', command=button_command).pack()
 
root.mainloop()
When I press the button with no input in the entry I get an error, but tkinter continues to run.
Error:
ValueError: invalid literal for int() with base 10: ''
If I type a 1 in the entry and press the number it appends a w.

My initial response was that time.sleep(text) was crashing because text was a str. I don't know if you changed your post or if I missed the int(text). But even if I pass a string to sleep() it does not crash the program. I get a type error:
Error:
TypeError: an integer is required (got type str)
But the program continues to run.

Can you provide more details? What are you running this on? What version of Python are you using? Can you provide more details about the "crashing". Is there an error message and traceback or does the window just close?