嗨,我正在用python做一个基于文本的冒险,在接受用户输入时遇到了问题,用户读取的文本在一个文本框中,而输入则放在一个输入框中。 我不知道如何让它等待用户按enter键或者等待实际GUI上的按钮被按下。在
我试过:
textin.bind('<Return>', get_input)
但由于某种原因我没法锻炼。在
我也尝试过:
^{pr2}$但是这使得GUI没有出现或者我没有正确使用它。在
另一件让我厌倦的事是:
import time
time.sleep(5.0)
但是同样的问题是GUI在倒计时之后才出现。在
我不能使用
input()
因为我正在使用图形用户界面,如果我使用它,那么图形用户界面将不会出现,直到之后或其他一些问题(我可能是错的,我仍然是新手)
我需要知道如何让它在用户按下enter键或GUI上的按钮时获取条目中的文本,所有这些都需要在一个函数中,以便主游戏可以等待他们输入命令。在
迄今为止的准则:
import tkinter as tk
def get_input(): #the button I put in had problems
player_command = textin.get() #if this function wasn't before it
root = tk.Tk()
frame = tk.Frame(root)
frame.grid(row = 0, column = 0)
textout = tk.Text(frame, width = 50, height = 23)
scroll = tk.Scrollbar(frame)
textin = tk.Entry(frame, width = 50)
button = tk.Button(frame, text = 'Submit', command = get_input)
textout.grid(row = 0, columnspan = 2, sticky = tk.W + tk.E)
textin.grid(row = 1, column = 0, sticky = tk.W + tk.E)
button.grid(row = 1, column = 1, sticky = tk.W + tk.E)
scroll.grid(row = 0, column = 1, sticky = tk.N + tk.S + tk.E)
scroll.config(command = textout.yview)
def main_menu():
textout.configure(state = 'normal')
textout.insert(tk.END, "Welcome to The Adventure\n")
textout.insert(tk.END, "What would you like to do?\n")
textout.insert(tk.END, "Continue\n")
textout.insert(tk.END, "New Game\n")
textout.insert(tk.END, "Help\n")
textout.configure(state = 'disabled')
while True:
if player_command == 'continue':
load_game() #other function
elif player_command == 'new game':
character_creation() #other function
elif player_command == 'help':
help_info() #other function
else:
textout.configure(state = 'normal')
textout.insert(tk.END, "Unknown command, type 'help' for hints")
textout.configure(state = 'disabled')
main_menu()
root.mainloop()
提前谢谢。在
下面是从文本框获取用户输入的示例。
注意,在这个例子中,Enter键用于获取
current line
上的字符串,submit
按钮用于打印当前保存的数据。还请注意,您可以添加类似的函数(类似于
stripLine
)来处理退格和类似的其他按键相关问题 更多 >
编程相关推荐