python按键持续按下响应_Python:等待enter键或按钮按下(基于文本的GUI冒险)

嗨,我正在用python做一个基于文本的冒险,在接受用户输入时遇到了问题,用户读取的文本在一个文本框中,而输入则放在一个输入框中。

我不知道如何让它等待用户按enter键或者等待实际GUI上的按钮被按下。在

我试过:textin.bind('', 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()

提前谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用`QDialog`来创建子界面,然后在主界面中调用子界面的`exec_()`方法来显示它。在子界面中,你可以重写`keyPressEvent`方法来检测用户是否按下Enter。如果是,你可以调用`accept()`方法来关闭子界面。以下是一个简单的示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QPushButton, QVBoxLayout, QLabel from PyQt5.QtGui import QKeyEvent class ChildDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) # 创建子界面上的控件 self.label = QLabel('Press Enter to close the dialog') layout = QVBoxLayout() layout.addWidget(self.label) self.setLayout(layout) def keyPressEvent(self, event: QKeyEvent): if event.key() == 16777220: # Enter的keyCode self.accept() class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建主界面上的控件 button = QPushButton('Show dialog', self) button.clicked.connect(self.show_dialog) layout = QVBoxLayout() layout.addWidget(button) central_widget = QWidget() central_widget.setLayout(layout) self.setCentralWidget(central_widget) def show_dialog(self): # 创建子界面并显示 dialog = ChildDialog(self) dialog.exec_() if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_() ``` 在这个示例代码中,我们首先创建了一个`ChildDialog`类来表示子界面。在这个类中,我们创建了一个`QLabel`控件来提示用户按下Enter来关闭子界面。我们还重写了`keyPressEvent`方法来检测用户是否按下Enter,如果是,我们就调用`accept()`方法来关闭子界面。 在`MainWindow`类中,我们创建了一个`QPushButton`控件来触发显示子界面的操作。当用户点击按钮时,我们创建一个`ChildDialog`对象并调用它的`exec_()`方法来显示它。因为`exec_()`方法会阻塞主线程,直到子界面被关闭,所以我们不需要显式地等待子界面被关闭。当用户按下Enter时,子界面会自动关闭,主线程继续执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值