I am somewhat new to Python (using 3.8 and PyCharm if that helps) and I have been trying to use tkinter to make a little text based game. Everything I have written so far works except for the part where I need a line to wait until a variable is assigned to execute. It is displaying a jumble of numbers and letters on the top of the window, but then it runs the rest of the code fine. However it never runs the final line properly and will not wait for the variable to be assigned.
Here is my current code:
import time
import tkinter
import tkinter as tk
from tkinter import *
from tkinter import simpledialog
window = tk.Tk()
T = tk.Text(window, height=20, width=90, bg="black", font="courier", fg="lightgreen", background="black")
T.pack()
T.insert(tk.END, "ELFLAB UNION TERMINAL 300")
window.after(2000, lambda: T.insert(tk.INSERT, "\nBooting"))
window.after(2500, lambda: T.insert(tk.END, "."))
window.after(3500, lambda: T.insert(tk.END, "."))
window.after(4500, lambda: T.insert(tk.END, "."))
window.after(6000, lambda: T.insert(tk.INSERT, "\nBooting"))
window.after(6500, lambda: T.insert(tk.END, "."))
window.after(7500, lambda: T.insert(tk.END, "."))
window.after(8500, lambda: T.insert(tk.END, "."))
window.after(10000, lambda: T.insert(tk.END, "\nSYSTEM INITIALIZED"))
window.after(11000, lambda: T.insert(tk.END, "\nWhat is your Name?"))
name1 = window.after(12000, lambda: simpledialog.askstring(title="USER CONSOLE", prompt="Enter your name"))
T.insert(tk.INSERT, lambda: T.insert(tk.INSERT, "\nHello " + name1 + ", I am Elfster, Your personal Terminal Assistant."))
tk.mainloop()
I have tried a lot of different methods for the bottom part.
window.after_close(window)
window.after(1000, lambda: T.insert(tk.INSERT, “\nHello “ + name1 + “, I am Elfster, your personal Terminal Assistant.”))
Or:
window.after_close(window)
T.insert(tk.INSERT, “\nHello “ + name1 + “, I am Elfster, your personal Terminal Assistant.”))
None of the codes have waited for the input. Does anyone have any ideas?
Tkinter waits till you destroy the dialog window when you call it at runtime. The problem here is, while tkinter initialized it iternally breaks with that rule. So the solution is to outsource your desired functionality in a function that can look like:
To run that code at the right time you can still use
.afterand a lambda expression that can look like: