简体   繁体   English

有没有办法让文本仅在分配对话框中的变量后才显示?

[英]Is there a way for a text to only show once a variable from a dialog box has been assigned?

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.我对 Python 有点陌生(如果有帮助,使用 3.8 和 PyCharm),我一直在尝试使用 tkinter 制作一个基于文本的小游戏。 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.它在 window 的顶部显示混乱的数字和字母,但随后它可以正常运行代码的 rest。 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. Tkinter 等到您在运行时调用对话框 window 时将其销毁。 The problem here is, while tkinter initialized it iternally breaks with that rule.这里的问题是,虽然 tkinter 初始化它总是违反该规则。 So the solution is to outsource your desired functionality in a function that can look like:因此,解决方案是将您想要的功能外包到 function 中,如下所示:

def ask():
    name1 = simpledialog.askstring(title="USER CONSOLE", prompt="Enter your name")
    T.insert(tk.INSERT, "\nHello " + name1 + ", I am Elfster, Your personal Terminal Assistant.")

To run that code at the right time you can still use .after and a lambda expression that can look like:要在正确的时间运行该代码,您仍然可以使用.afterlambda 表达式,如下所示:

window.after(12000, lambda: ask())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM