【问题标题】:I'm trying to create a tkinter output window for a text based application我正在尝试为基于文本的应用程序创建一个 tkinter 输出窗口
【发布时间】:2017-12-20 00:02:21
【问题描述】:

我有一个脚本,它不断地接收文本并输出文本(它是一个基于文本的游戏)

我想通过 tkinter GUI 而非控制台运行它

Python : Converting CLI to GUI

这个问题完美回答了如何将“打印”转换为 GUI 插入。

问题是我的游戏显然运行了很多循环,这搞砸了“app.mainloop()”,因为它要么永远不会运行(然后 GUI 永远不会出现),要么你先运行它,然后它不会让其他任何东西运行。

我想我可以尝试并以某种方式错开这些循环,但这似乎很老套。我也可以尝试修改我的整个代码库以在 app.mainloop() 中运行,但我真正认为我需要的是多个线程。问题是,我不知道如何做到这一点。

还有一些其他问题,但它们要么不起作用,要么没有多大意义: Tkinter with multiple threads

Run process with realtime output to a Tkinter GUI

谢谢。

编辑:极其简化的代码:

def moveNorth():
    print('You have moved north')

def interpreter(command):
    if command == 'go north':
         moveNorth()
    else:
         print('invalid command')

def listener():
     playerchoice = sys.stdin.readline().strip()
     return playerchoice

if __name__ == '__main__':
    print('Welcome')
    while playing:
        interpreter(listener())

【问题讨论】:

  • while 循环是指while bad_answer: ask_question()
  • 是的,主要的是我的翻译。它基本上不断地倾听玩家的输入,然后根据它做事。现在我想起来了,这可能是唯一导致问题的循环,因为它是唯一的无限循环。不过我的代码很长,所以我可能忘记了一些东西。
  • 发布你的一些代码,或者至少是一个Minimal, Complete, and Verifiable example。尝试尽可能多地删除代码,同时保持 bug 完整
  • 我真的没有任何错误,因为我不知道如何开始。但是我添加了一个非常(!)简化的代码版本。

标签: python multithreading python-3.x tkinter


【解决方案1】:

我认为你可能让它变得比它需要的更复杂。

至少对于 Tkinter 来说,将控制台交互改为 GUI 交互非常简单。

我能给出的最简单的例子是使用Entry 字段作为用户输入,使用Text 小部件作为输出。

这是一个使用 Tkinter 将基于控制台的游戏移至 GUI 的简单示例。

控制台猜数字游戏:

import random

print("simple game")
print("-----------")

random_num = random.randint(1, 5)
print(random_num)

x = True

while x == True:
    #Input for user guesses.
    guess = input("Guess a number between 1 and 5: ")

    if guess == str(random_num):
        #Print answer to console.
        print("You win!")
        x = False
    else:
        print("Try again!")

这是同一游戏的 Tkinter GUI 示例:

import tkinter as tk
import random


root = tk.Tk()

entry_label = tk.Label(root, text = "Guess a number between 1 and 5: ")
entry_label.grid(row = 0, column = 0)

#Entry field for user guesses.
user_entry = tk.Entry(root)
user_entry.grid(row = 0, column = 1)

text_box = tk.Text(root, width = 25, height = 2)
text_box.grid(row = 1, column = 0, columnspan = 2)

text_box.insert("end-1c", "simple guessing game!")

random_num = random.randint(1, 5)

def guess_number(event = None):
    #Get the string of the user_entry widget
    guess = user_entry.get() 

    if guess == str(random_num):
        text_box.delete(1.0, "end-1c") # Clears the text box of data
        text_box.insert("end-1c", "You win!") # adds text to text box

    else:
        text_box.delete(1.0, "end-1c")
        text_box.insert("end-1c", "Try again!")

        user_entry.delete(0, "end")
# binds the enter widget to the guess_number function
# while the focus/cursor is on the user_entry widget
user_entry.bind("<Return>", guess_number) 

root.mainloop() 

如您所见,GUI 的代码更多,但大部分是 GUI 设计。

您需要更改的主要部分是在您的答案中使用输入与输入以及在您的响应中使用插入与打印。剩下的只是设计的东西。

如果您想保持问题的连续性,您可以使用新问题更新标签,或者您甚至可以为每个新问题使用 tkinters askstring 函数。有很多选择。

主要是获取用户答案的​​值,使用该答案对问题进行测试,然后将结果打印到文本框。

【讨论】:

  • @JaneDoe:不客气。你会发现 tkinter 有很多有用的 GUI 小部件。对于任何基于文本或基于图像的游戏,您都可以使用 Tkinter。如果您想玩 2D 或 3D 游戏,请查看 pygame。
猜你喜欢
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-02
相关资源
最近更新 更多