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

2024-06-23 19:11:23 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在用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()

提前谢谢。在


Tags: 用户inputgetguifunctionframecommandtk
1条回答
网友
1楼 · 发布于 2024-06-23 19:11:23

下面是从文本框获取用户输入的示例。
注意,在这个例子中,Enter键用于获取current line上的字符串,submit按钮用于打印当前保存的数据。

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()


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

相关问题 更多 >

    热门问题