-1

I am trying to make a label, but I can't define the root variable! Please help me!

Update: I am using an online Python editor called repl.it

I am using Python 3.0

This is my code:

import time
from tkinter import *

root = Tk()

def w(t):
  if t == t:
    time.sleep(t)
print("In the following story, you are a mouse. Please fill out the following information (the story will use this name and gender).")
w(2)
print('\n')

while(True):
  name = input("Your Name:")
  if name.isalpha():
    break
  elif name != str:
    print('\n')
    print("Error! Please enter a name!")
    print('\n')

w(0.5)
print('\n')

while(True):
  gender = input("Your Gender (Male / Female):").lower()
  if gender == "male":
    heorshe = "he"
    hisher = "his"
    himher = "him"
    himselfherself = "himself"
    HeShe = "He"
    break
  elif gender == "female":
    heorshe = "she"
    hisher = "her"
    himher = "her"
    himselfherself = "herself"
    HeShe = "He"
    break
  else:
    print('\n')
    print("Error Please enter a valid gender!")
    print('\n')

w(0.5)
print('\n')
input("Press Enter to Begin The Story")
w(0.5)
print('\n')
print("The Lion and The Mouse")
print("A Short Tale by Aesop")
w(1)
print('\n')
print("Once upon a time in a secluded jungle, there was a mouse.")
w(1.25)
print('\n')
print("%s was a very curious little mouse. Whenever %s saw something that caught %s eye, %s would always go and investigate." % (name, heorshe, hisher, heorshe))
w(2.5)
print('\n')
print("One day, %s came across a mighty lion. At first the lion didn't see %s, but due to %s curiosity, %s revealed %s. The lion had spotted %s." % (heorshe, himher, hisher, heorshe, himselfherself, himher))
w(3.25)
print('\n')
print("Hungry for a meal, the lion lunged at the mouse.")
print('\n')
w(1.75)
print("With the mouse in the lion's grip, the lion licked his lips, ready to bite in.")
print('\n')
w(1.5)
while(True):
  mercyoryell = input("Do you 'ASK FOR MERCY' or 'YELL HELP'?").lower()
  if mercyoryell == "ask for mercy":
    print('\n')
    print("Mouse: Oh please, great lion, let me go free! I promise I can repay you later!")
    w(1)
    print('\n')
    print("Lion: Ha! The thought of a mere mouse repaying me? I shall let you go, but only because you have given me such a great laugh!")
    w(.75)
    print('\n')
    print("The lion let the mouse go, and the mouse scurried away as fast as %s could." % (heorshe))
    print('\n')
    break
  elif mercyoryell == "yell help":
    print('\n')
    print("Mouse: HELLLLPPPP! I AM GOING TO GET EATEN BY A NASTY LION! SOMEBODY HELP ME! ANYBODY! AHHHH!!!")
    w(1.35)
    print('\n')
    print("Alas, the mouse's attempts were not successful. The lion quickly gobbled %s up." % (himher))
    w(1.5)
    print('\n')
    label = Label(root, text="The End!", bg="white", fg="black", font=("Comic Sans MS", 35, "bold"), height=75, width=75)
    print(label)
  else:
    print('\n')
    print("Error. Please type either ASK FOR MERCY or YELL HELP.")
    print('\n')
  w(1)
  print("A couple of days later, the mouse explores the forest again. %s comes across the lion, all bundled up in rope, moaning in pain." % (HeShe))
  print('\n')
  w(1.5)
  print("Mouse: What happened? How did you get caught in this net?")
  print('\n')
  w(1)
  print("Lion: The stupid hunters set up a net right outside of my den! Thats how! Now hurry up and get me out of here before they come back, you stupid mouse!")
  w(1.75)
  print('\n')
  while(True):
    lastscene = input("Do you 'HELP HIM OUT' or 'IGNORE HIM'?").lower()
    if lastscene == "help him out":
      w(.5)
      print('\n')
      print("The mouse starts chewing on the rope. %s finally makes a hole big enough for the lion to escape from, and the lion jumps out of the net")
      w(1.5)
      print('\n')
      break
    elif lastscene == "ignore him":
      w(.5)
      print('\n')
      print("Mouse: Why would I help you when you show me no kindness? I'll leave you to the hunters, stupid lion!")
      w(.75)
      print('\n')
      print("The mouse runs away from the lion, ignoring him.")
      print('\n')
      w(1.5)
      print("Later on that day, a deafening growl is heard. The lion had been found by the hunters, and was killed.")
      w(1.5)
      print('\n')
      label = Label(root, text="The End!", bg="white", fg="black", font=("Comic Sans MS", 35, "bold"), height=75, width=75)
      print(label)
    else:
      w(.5)
      print('\n')
      print("Error! Please enter either HELP HIM OUT or IGNORE HIM !")
      print('\n')
w(.5)
print("Lion: Thank you so much, kind mouse! I saved you, and you did indeed repay me! I will now forever show my gratitude towards you, my humble friend!")
w(1.5)
print('\n')
print("No act of kindness, no matter how small, is ever wasted.      -Aesop")
print('\n')
w(1.5)
label = Label(root, text="The End!", bg="white", fg="black", font=("Comic Sans MS", 35, "bold"), height=75, width=75)
print(label)

And finally, this is the error message I get when I run the code:

    Traceback (most recent call last):
  File "python", line 4, in <module>
_tkinter.TclError: no display name and no $DISPLAY environment variable

I have no idea what to do, and none of the several guides I've looked up have helped me.

  • Have you checked https://stackoverflow.com/questions/19309085/no-display-name-and-no-display-environment-variable-using-tkinter-through-ssh ? Or any google result from that error message? – Lafexlos Jun 08 '17 at 12:45
  • @Lafexlos That isn't what I'm trying to do though. – Still Here Coding Jun 08 '17 at 12:52
  • this site has [143 similar questions](https://stackoverflow.com/search?q=%5Btkinter%5D+no+display+name+and+no+%24DISPLAY) specifically related to tkinter. Please do a little research before asking. – Bryan Oakley Jun 08 '17 at 13:20
  • @BryanOakley This doesn't help me. I would like to know how to fix this specific problem. – Still Here Coding Jun 08 '17 at 14:38
  • @MikeTheProgrammer: Bryan was pointing out that your issues in your question have been asked many times. Just because your question has different content in the code it is still a question that has been repeated many many times. With just a small amount of googling or searching this site you would have found your answers. – Mike - SMT Jun 08 '17 at 14:41
  • What happens when you type the command `xclock` or `xterm` at the prompt? Are they able to open up a window? – Bryan Oakley Jun 08 '17 at 15:09
  • @BryanOakley I'm using an online editor for python as i do not have it downloaded. – Still Here Coding Jun 08 '17 at 17:00
  • 1
    @MikeTheProgrammer: that's a huge detail to have left out. You can't use tkinter via a web server. – Bryan Oakley Jun 08 '17 at 17:39

2 Answers2

1

In a comment you wrote:

I'm using an online editor for python as i do not have it downloaded.

You can't run tkinter via a web server. There is simply nothing you can do to make that work.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

Repl.it doesn't currently support tkinter but it supports a GUI library called REMI. You can see an example here: https://repl.it/@amasad/tictactoe

Amjad Masad
  • 4,035
  • 1
  • 21
  • 20