简体   繁体   中英

Why does my program not respond when I run it

I recently started python, and realize this is probably a dumb question to be asking, but I was trying to make a choice based game using a window. I used graphics.py to display my text. When I run the code, the window opens but is in the "not responding state", and I couldn't find out what was wrong. also there was no error code

I basically copied everything from a video from left peel. However, is vids are very outdated so there is probably an error somewhere, but there are no more sites or vids that explain graphics.py

Here is the code:

from graphics import *


def main():
    win = GraphWin("win", 1300, 700)
    #win.setBackground(color_rgb(255, 0, 0))

    txt = Text(Point(650, 120), 'This was text')
    txt1 = Text(Point(650, 135), "This was text")
    txt2 = Text(Point(650, 150), "This was text")
    txt3 = Text(Point(650, 165), "This was text")
    txt4 = Text(Point(650, 180), "This was text "
                                 "This was text.")

    img = Image(Point(650, 450), "a picture (.gif)")
    nxt = Text(Point(1090, 590), "Type next for next")
    txt.draw(win)
    txt1.draw(win)
    txt2.draw(win)
    txt3.draw(win)
    txt4.draw(win)

    img.draw(win)
    inputb = Entry(Point(1200, 590), 10)
    inputb.draw(win)
    nxt.draw(win)

    s = inputb.getText()

    command = inputb.getText()
    while True:
        s = inputb.getText()
        if s == 'next':
            the program stopped working from here, so i just put a print("")

main() 

Despite siting atop tkinter, Zelle Graphics has no user access to the event manager. (Unlike Python turtle which also sits atop tkinter.) And the Entry object really exposes this shortcomming.

Two ways people make it have any state is to either run an infinite loop processing input constantly or use mouse clicks to advance the state. Since you're getting the "not responding" message, I assume you're running under some sort of development IDE, like Anaconda, and it does not like your runaway while True: loop. So we need to switch to mouse clicks. Here's a simple example:

from graphics import *

win = GraphWin()

entry = Entry(Point(100, 100), 10)
entry.draw(win)

text = Text(Point(100, 150), 'This is text')
text.draw(win)

while True:
    win.getMouse()

    string = entry.getText()

    if string.lower() == 'quit':
        break

    text.setText(string.upper())

You can type in the Entry widget, and then when you click the mouse, your input will show up in the Text widget, but in all upper case. If you enter "quit", the program will exit. Your user needs to know to click after any input into any entry field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM