Python:等待enter键或按钮按下(GUI基于文本的冒险)

Python:等待enter键或按钮按下(GUI基于文本的冒险),python,input,tkinter,Python,Input,Tkinter,您好,我正在用python进行一次基于文本的冒险,在接受用户输入时遇到了问题,用户读取的文本在一个文本框中,输入在一个输入框中。 我不知道如何让它等待用户按enter键或在实际GUI上按下按钮 我试过: textin.bind('<Return>', get_input) 但是这使得GUI没有显示出来,或者我没有正确地使用它 另一件让我感到厌倦的事情是: import time time.sleep(5.0) 但同样的问题是GUI在倒计时后才显示 我不能用 input()

您好,我正在用python进行一次基于文本的冒险,在接受用户输入时遇到了问题,用户读取的文本在一个文本框中,输入在一个输入框中。 我不知道如何让它等待用户按enter键或在实际GUI上按下按钮

我试过:

textin.bind('<Return>', get_input)
但是这使得GUI没有显示出来,或者我没有正确地使用它

另一件让我感到厌倦的事情是:

import time
  time.sleep(5.0)
但同样的问题是GUI在倒计时后才显示

我不能用

input()
当我使用GUI时,如果我使用GUI,那么GUI在出现问题或其他问题之前不会显示(我可能错了,我对其中一些问题还是新手)

我需要知道如何让它在用户按下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键用于获取
当前行上的字符串
submit
按钮用于打印当前保存的数据。

将Tkinter作为tk导入
类别测试(对象):
定义初始化(自,根):
self.laststat=0#只需检查多个按钮,不删除数据
self.currString=“”
self.tempString=“”
self.text=tk.text(根)
self.text.bind(“”,self.stripLine)
self.text.pack(side=tk.LEFT)
self.text.focus()
self.button=tk.button(根,文本='Submit',命令=self.printData)
自动按钮包(侧=左侧)
def带线(自身、事件):
val=({k!r}.format(k=event.char))[1:-1]
如果val='\\r'和self.laststat==0:
self.currString=self.tempString
self.tempString=“”
self.laststat=1
伊里夫·瓦尔\\r':
self.tempString+=val
self.laststat=0
def打印数据(自身):
打印“当前字符串为:”+self.currString
#打印“临时字符串为:”+self.tempString
root=tk.tk()
A=测试(根)
root.mainloop()

还请注意,您可以添加类似的函数(类似于
带状线
)来处理退格和类似的其他按键

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()
import Tkinter as tk

class Test(object):
    def __init__(self, root):
        self.laststat=0#just to check multiple <Enter key> press do not delete the data
        self.currString=""
        self.tempString=""
        self.text = tk.Text(root)
        self.text.bind('<Key>', self.stripLine)
        self.text.pack(side=tk.LEFT)
        self.text.focus()
        self.button = tk.Button(root, text = 'Submit', command = self.printData)
        self.button.pack(side=tk.LEFT)

    def stripLine(self, event):
        val=('{k!r}'.format(k = event.char))[1:-1]
        if val=='\\r' and self.laststat==0:
            self.currString=self.tempString
            self.tempString=""
            self.laststat=1
        elif val!='\\r':
            self.tempString+=val
            self.laststat=0


    def printData(self):
        print 'Current String is :'+self.currString
        #print 'temp String is :'+self.tempString

root = tk.Tk()
A = Test(root)
root.mainloop()