如何在 Python 3 中为 tkinter 定义根?我正在使用在线 Python 编辑器 [英] How do I define root for tkinter in Python 3? I'm using an online Python editor

查看:22
本文介绍了如何在 Python 3 中为 tkinter 定义根?我正在使用在线 Python 编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作标签,但无法定义根变量!请帮我!

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

更新:我正在使用一个名为 repl.it 的在线 Python 编辑器

我使用的是 Python 3.0

I am using Python 3.0

这是我的代码:

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

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

w(0.5)
print('
')

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('
')
    print("Error Please enter a valid gender!")
    print('
')

w(0.5)
print('
')
input("Press Enter to Begin The Story")
w(0.5)
print('
')
print("The Lion and The Mouse")
print("A Short Tale by Aesop")
w(1)
print('
')
print("Once upon a time in a secluded jungle, there was a mouse.")
w(1.25)
print('
')
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('
')
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('
')
print("Hungry for a meal, the lion lunged at the mouse.")
print('
')
w(1.75)
print("With the mouse in the lion's grip, the lion licked his lips, ready to bite in.")
print('
')
w(1.5)
while(True):
  mercyoryell = input("Do you 'ASK FOR MERCY' or 'YELL HELP'?").lower()
  if mercyoryell == "ask for mercy":
    print('
')
    print("Mouse: Oh please, great lion, let me go free! I promise I can repay you later!")
    w(1)
    print('
')
    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('
')
    print("The lion let the mouse go, and the mouse scurried away as fast as %s could." % (heorshe))
    print('
')
    break
  elif mercyoryell == "yell help":
    print('
')
    print("Mouse: HELLLLPPPP! I AM GOING TO GET EATEN BY A NASTY LION! SOMEBODY HELP ME! ANYBODY! AHHHH!!!")
    w(1.35)
    print('
')
    print("Alas, the mouse's attempts were not successful. The lion quickly gobbled %s up." % (himher))
    w(1.5)
    print('
')
    label = Label(root, text="The End!", bg="white", fg="black", font=("Comic Sans MS", 35, "bold"), height=75, width=75)
    print(label)
  else:
    print('
')
    print("Error. Please type either ASK FOR MERCY or YELL HELP.")
    print('
')
  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('
')
  w(1.5)
  print("Mouse: What happened? How did you get caught in this net?")
  print('
')
  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('
')
  while(True):
    lastscene = input("Do you 'HELP HIM OUT' or 'IGNORE HIM'?").lower()
    if lastscene == "help him out":
      w(.5)
      print('
')
      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('
')
      break
    elif lastscene == "ignore him":
      w(.5)
      print('
')
      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('
')
      print("The mouse runs away from the lion, ignoring him.")
      print('
')
      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('
')
      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('
')
      print("Error! Please enter either HELP HIM OUT or IGNORE HIM !")
      print('
')
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('
')
print("No act of kindness, no matter how small, is ever wasted.      -Aesop")
print('
')
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.

推荐答案

在您写的评论中:

我正在使用 python 在线编辑器,因为我没有下载它.

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

您不能通过网络服务器运行 tkinter.您无法做任何事情来完成这项工作.

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

这篇关于如何在 Python 3 中为 tkinter 定义根?我正在使用在线 Python 编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆