Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:13:54+00:00 2026-06-18T08:13:54+00:00

The following is my short script. It is meant to print right left and

  • 0

The following is my short script. It is meant to print right left and up when those arrow keys are held, but I don’t know why it does not work.

import Tkinter as tk

right = False
left = False
up = False

def keyPressed(event):
    if event.keysym == 'Escape':
        root.destroy()
    if event.keysym == 'Right':
        right = True
    if event.keysym == 'Left':
        left = True
    if event.keysym == 'Up':
        up = True

def keyReleased(event):
    if event.keysym == 'Right':
        right = False
    if event.keysym == 'Left':
        left = False
    if event.keysym == 'Up':
        up = False

def task():
    if right:
        print 'Right'
    if left:
        print 'Left'
    if up:
        print 'Forward'
    root.after(20,task)

root = tk.Tk()
print( "Press arrow key (Escape key to exit):" )

root.bind_all('<Key>', keyPressed)
root.bind_all('<KeyRelease>', keyReleased)
root.after(20,task)

root.withdraw()
root.mainloop()

The issue began when I started using root.after().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:13:55+00:00Added an answer on June 18, 2026 at 8:13 am

    In python, functions create a new scope. If a variable isn’t found within the function’s scope, python looks in the outer (module/file) scope for the variable. You add variables into the current scope with assignment. This all means that:

    right = False
    def func():
        right = True
    func()
    print (right)  #right is still False in the outer scope.
    

    In order to actually modify the variable in an outer scope, you need to tell python that you want to do something like that explicitly:

    right = False
    def func():
        global right
        right = True
    func()
    print (right)
    

    This works, but it’s not considered good practice because you’re changing the state of your program. Now the value of right depends on whether you’ve called a function which is a bit unsettling.

    A better way to share data between function calls is to use a class. Then methods (functions bound to an instance of the class) can change the state of that single instance, but the rest of your program can continue as if nothing happened.

    class Foo(object):
        def __init__(self):
            self.right = False
        def func(self):
            self.right = True
    
    a = Foo() #calls __init__ implicitly
    print(a.right)  #should be False -- We set this in __init__
    a.func()  #change state of `a`
    print(a.right)  #Now it's True!
    

    Here’s a slightly more “classy” version of your code:

    import Tkinter as tk
    
    class App(object):
        def __init__(self):
            self.right = False
            self.left = False
            self.up = False
    
        def keyPressed(self,event):
            print "HERE"
            if event.keysym == 'Escape':
                root.destroy()
            elif event.keysym == 'Right':
                self.right = True
            elif event.keysym == 'Left':
                self.left = True
            elif event.keysym == 'Up':
                self.up = True
    
        def keyReleased(self,event):
            if event.keysym == 'Right':
                self.right = False
            elif event.keysym == 'Left':
                self.left = False
            elif event.keysym == 'Up':
                self.up = False
    
        def task(self):
            if self.right:
                print 'Right'
            elif self.left:
                print 'Left'
            elif self.up:
                print 'Forward'
            root.after(20,self.task)
    
    application = App()
    root = tk.Tk()
    print( "Press arrow key (Escape key to exit):" )
    
    root.bind_all('<Key>', application.keyPressed)
    root.bind_all('<KeyRelease>', application.keyReleased)
    root.after(20,application.task)
    
    root.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the following question has been asked many times: <script> var myVar
Ok, so I've worked through a number of bugs in the following short script.
the following short test script <?php define('DBHOST', '/tmp'); define('DBNAME', 'XXX'); define('DBUSER', 'XXX'); define('DBPASS', 'XXX');
I've written the following short python script to download flv videos using cclive on
I have the following script which runs fine but I think a) it is
I can't figure out the problem in the following short script which should compare
The following short Fortran90 program crashes as long as it contains the MPI_GET call.
Consider the following short program: #include <type_traits> #include <iostream> using namespace std; template <typename
I have the following short version of a tsql if else if.. IF @var
I have been following a short tutorial to build a tab menu on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.