Text based tkinter game loop only seems to work once


This Content is from Stack Overflow. Question asked by russj

I have a python based text adventure game I am trying to turn into a tkinter GUI.
Python game is here, if interested: https://github.com/Russjas/annoying-game/blob/main/Adventure_Game.py

For the GUI:

import tkinter as tk

key = 0
dagger = 0
tome = 0
ruby = 0
user_input = ''

def write(text):
        Console.insert('1.0', text)
def startroom():
        global key, ruby, tome, dagger, place
        place = 1
        key = 0
        dagger = 0
        tome = 0
        ruby = 0
        write('The room is panelled in dark wood, with dirt in the corners and mysterious stains on the walls. The room has 4 doors, one for each cardinal point. Which one do you want to take? (N, S, E, W) ' )
        write('You awaken in a mysterious room, lit by flickering candlelight.n in your right hand is a compass, in your left is a list of 4 items "tome, key, ruby, dagger"')

def roomfinder():
    global place
    if place == 'start':
        if user_input == 'start':
            write('Please enter a valid response')
    elif place == '1':
        if user_input.lower() == 'n':
            write('You fall to your doom. There was no room here! n n')
    #whole bunch of other elif statements for input where place == 1 and further elif for other values of place

root1 = tk.Tk() # name of the main window in the GUI
root1.title('An Annoying Game')

label = tk.Label(root1, text='testGUI', font=('Arial', 18)) 
label.pack(padx=50, pady=50) #.pack puts it into the window

Console = tk.Text(root1, height=10, padx=5, pady=5, wrap=tk.WORD)

entrybox = tk.Entry(root1)
entrybox.bind("<KeyPress>", shortcut)

write('Are you sure you want to continue? nType start to begin. nIf it all gets too much, quit at any time with Q')
place = 'start'

So when I run this, the gui opens, if I type start press return I get the startroom() text (as expected), but if I type more input nothing further happens except console and entrybox get blanked. No errors are raised

I know that user_input and place are modified correctly (I have printed them to the console after running the loop), but even if ”’place == 1”’ and “user_input == ‘n'” on the return key it does not “write(‘You fall to your doom. There was no room here! n n’).”


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?