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 8466763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:26:18+00:00 2026-06-10T15:26:18+00:00

I’m making a little toy command window with Tk, and currently trying to make

  • 0

I’m making a little toy command window with Tk, and currently trying to make it copy some interpreter behavior.

I’d never scrutinized the interpreter before, but it’s decisions on when to print a value are a little mystifying.

>>> 3 + 4  # implied print(...)
7
>>> 3      # implied print(...)
3
>>> a = 3  # no output, no implied print(...), bc result is None maybe?
>>> None   # no output, no print(...) implied... doesn't like None?
>>> print(None)  # but it doesn't just ban all Nones, allows explicit print()
None
>>> str(None) # unsurprising, the string 'None' is just a string, and echoed
'None'

The goal is to mimic this behavior, printing some Nones, not others (made slightly more complicated because I’m not entirely sure what the rules are).

So, turning to my program, I have history_text and entry_text, which are StringVar()s that control a label above an entry box in the Tk window. Then the following event is bound to the Return key, to process commands and update the history with the result.

def to_history(event):
    print("command entered")  # note to debugging window

    last_history = history_text.get()

    # hijack stdout
    buffer = io.StringIO('')
    sys.stdout = buffer

    # run command, output to buffer
    exec(entry_text.get())

    # buffered output to a simple string
    buffer.seek(0)
    buffer_str = ''
    for line in buffer.readlines():
        # maybe some rule goes here to decide if an implied 'print(...)' is needed
        buffer_str = buffer_str + line + '\n'

    # append typed command for echo
    new_history = entry_text.get() + '\n' + buffer_str

    # cleanup (let stdout go home)
    sys.stdout = sys.__stdout__
    buffer.close()

    history_text.set(last_history + "\n" + new_history)
    entry_text.set('')

As is, it does not provide any output for a simple entry of ‘3’ or ‘None’ or even ‘3 + 4’. Adding an implied print() statement all the time seems to print too often, I don’t skip the print for ‘None’ or ‘a = 3’ type statements.

I found some documentation for sys.displayhook, which seems to govern when the interpreter will actually display a result, but I’m not sure how to use it here. I thought I could just wrap sys.displayhook() around my exec() call, and have it do all this work for me… but found that it does not imply print() statements for statements like ‘3 + 4’ or ‘3’.

Any suggestions? Am I on the right track with sys.displayhook?

  • 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-10T15:26:20+00:00Added an answer on June 10, 2026 at 3:26 pm

    The interpreter prints out repr(result) only if result is not None.

    There are no “implied prints” like you thought.

    • 3 + 4 results to 7, so repr(7) is printed
    • a = 3 is an assignment, I think nothing is printed because it does not work with eval
    • None results to None, so nothing is printed
    • print(None) results to None (because the print function returns nothing), so nothing is printed. However, the print function itself printed the None.

    I honestly didn’t read your code, but here’s a function that takes a string with code and produces the same output as the interpreter would:

    def interactive(code):
        try:
            result = eval(code)
            if result is not None:
                print(repr(result))
        except SyntaxError:
            exec(code)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.