Python Forum
[Tkinter] Clicking on the button crashes the TK window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Clicking on the button crashes the TK window
#1
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()
Yoriz write Mar-06-2022, 05:56 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 531 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  GUI crashes flash77 3 929 Jul-26-2023, 05:09 PM
Last Post: flash77
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,874 May-25-2023, 07:37 PM
Last Post: deanhystad
  [PyQt] App crashes when reopening a subwindow JayCee 13 5,091 Aug-04-2021, 01:51 AM
Last Post: JayCee
  [PyGUI] My GUI crashes after command MLGpotato 1 1,906 Feb-21-2021, 03:17 PM
Last Post: deanhystad
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 9 13,622 Jan-18-2021, 10:21 PM
Last Post: Serafim
  [PyQt] Received RunTimeError after script concludes, closing Dialog Window (clicking x-out) skipper0802 0 2,576 Oct-09-2020, 09:23 PM
Last Post: skipper0802
  [PyQt] PyQT5 Thread crashes after a while Suresh 0 1,988 Jul-21-2020, 07:53 AM
Last Post: Suresh
  Closing window on button click not working kenwatts275 4 3,804 May-03-2020, 01:59 PM
Last Post: deanhystad
  Need tkinter help with clicking buttons pythonprogrammer 2 2,476 Jan-03-2020, 04:43 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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