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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:53:51+00:00 2026-06-03T16:53:51+00:00

I am attempting to call an external function from the press of a button

  • 0

I am attempting to call an external function from the press of a button in a Tkinter GUI; the function ‘insert’s into a Text element. This works perfectly; as long as it is not followed closely by time.sleep(). If it isn’t followed, the text updates correctly; if it is, the text does not update, though the function still seems to be running the loop, as it will happily print text to the console.

Here’s my example script:

from Tkinter import *
from time import sleep

def test_function(gui_self):
    while True:
        print "Debug message"
        gui_self.main_.insert(END, "Test")


class GUI:
    def __init__(self, master):
        frame = Frame(master, width=300, height=500)
        frame.pack()
        self.main_ = Entry(frame, width=80, justify=CENTER)
        self.main_.pack()

        test_function(self)

root = Tk()
application = GUI(root)
root.mainloop()

This works well. However, changing test_function to the following seems to mess things up:

def test_function(gui_self):
    while True:
        print "Debug message"
        gui_self.main_.insert(END, "Test")
        sleep(5)

Any suggestions to fix whatever it is that I am doing wrong here? I’m sure it’s just an issue that I, as a fairly basic level user, am not using the correct syntax for.

As a side note, the ‘sleep’ command also seems to freeze the program entirely, which I find rather annoying and unattractive. Is there a method to pause the loop for a variably set (eg, var = 1(second)) amount of time, other than ‘pipe’ing threads, that would be efficient and more.. clean?

Thanks.

  • 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-03T16:53:52+00:00Added an answer on June 3, 2026 at 4:53 pm

    I completely agree with Nick Craig-Wood’s answer — don’t put the main thread of your gui to sleep. Generally, you want to call after which registers a function to be called after (approximately) X milliseconds. Here’s an example that will call test_function every 5 seconds. Every time test_function is called, it will re-register itself to be called 5 seconds later.

    def test_function(gui_self):
        print "Debug message"
        gui_self.main_.insert(END, "Test")
        gui_self.after(5000,test_function,gui_self)
        #the line below also should work.  As long as the item is a Tkinter widget
        #gui_self.main_.after(5000,test_function,gui_self) 
    

    Also, completely unrelated, (but as a matter of style) — is there a reason you’re using main_ in GUI instead of just main? Generally, trailing underscores are appended to aviod name-clashes with python keywords. As far as I know, main isn’t a keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time attempting to call an ASP.NET page method from jQuery.
I'm attempting to call a function inside of a lua file called test2.lua This
I am attempting to call a C++ function in a DLL from C# via
I'm attempting to call startActivity(myIntent) from the click of a menu button but my
I'm attempting to call a function in the ROOT plotting package that accepts three
I am attempting to call the Apache Commons StringUtils.join() method from within a Groovy
Attempting to insert an escape character into a table results in a warning. For
I'm attempting to call a method written in C++/CLI from C#. The C++/CLI code
I am attempting to call an ASP.NET page from a classic ASP page on
I am getting an error when attempting to call a stored proc from 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.