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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:18:57+00:00 2026-05-26T10:18:57+00:00

I’m having some problems with the global variables: #Dev by Mkee import Tkinter as

  • 0

I’m having some problems with the global variables:

#Dev by Mkee
import Tkinter as tk
import sys, random
globvar = 0
class Shuffle(object):
    def __init__(self,master=None):
        self.master=master
        self.text=tk.Label(master, text="Type in something:")
        self.box=tk.Entry(master)
        self.buttn = tk.Button(master, text="Done", width=10, command=self.calc())
        self.result = tk.Label(self.master, text=globvar)
    def hide(self):
        self.text.pack_forget()
        self.box.pack_forget()
        self.buttn.pack_forget()
        self.result.pack_forget()
    def show(self):
        self.text.pack(padx=2, pady=2)
        self.box.focus_set()
        self.box.pack(side=tk.TOP, padx=3, pady=3)
        self.buttn.pack(side=tk.TOP, padx=3, pady=3)
        self.result.pack(side=tk.TOP, padx=3, pady=3)
    def calc(self):
        word = self.box.get()
        while len(word) != 0:
            i = random.randrange(0, len(word))
            global globvar
            globvar = word[i] 
            word = word[0:i] + word[i+1:]


class MainApp(object):
    def __init__(self,master=None):
        self.master=master
        app=self.app=tk.Tk()
        app.title("Mkee's Tools")
        app.geometry('500x500')
        menubar=tk.Menu(app)
        self.shuffle=Shuffle(master)
        self.current=self.shuffle
        program_menu=tk.Menu(menubar)
        program_menu.add_command(label='Shuffle',command=self.show_shuffle)
        menubar.add_cascade(label='Programs', menu=program_menu)
        app.config(menu=menubar)
    def show_shuffle(self):
        self.current.hide()
        self.current=self.shuffle
        self.shuffle.show()
    def show_buttons(self):
        self.current.hide()
        self.current=self.buttons        
        self.buttons.show()

def main():
    m=MainApp()
    m.app.mainloop()

if __name__=='__main__':
    main()

I get the problem when I click the buttonself.buttn = tk.Button(master, text="Done", width=10, command=self.calc()). I want to make the self.result = tk.Label(self.master, text=globvar) show the globvar taken from calc, but instead it shows the globvar that is set before the classglobvar = 0.
What am I doing wrong?

  • 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-05-26T10:18:57+00:00Added an answer on May 26, 2026 at 10:18 am
    1. To make the Label update, set textvariable=self.globvar, where
      self.globvar = tk.StringVar(). The Label text is updated by calling self.globvar.set.
    2. Change command=self.calc() to command=self.calc (without
      parentheses). With parentheses, self.calc() is called at the time
      self.buttn is defined. And command gets set to the return value of
      self.calc(), which is None. Nothing happens when the button is
      pressed because command equals None. Without parentheses,
      command is set to the function object self.calc, and the
      function is called (correctly) whenever the button is pressed.
    3. Try to avoid using global variables. It will help Shuffle to be self-contained, more easily reusable and importable.

    import Tkinter as tk
    import sys, random
    
    class Shuffle(object):
        def __init__(self,master=None):
            self.master=master
            self.text=tk.Label(master, text="Type in something:")
            self.box=tk.Entry(master)
            self.buttn = tk.Button(master, text="Done", width=10, command=self.calc)
            self.globvar = tk.StringVar()
            self.globvar.set(0)
            self.result = tk.Label(self.master, textvariable=self.globvar)
        def hide(self):
            self.text.pack_forget()
            self.box.pack_forget()
            self.buttn.pack_forget()
            self.result.pack_forget()
        def show(self):
            self.text.pack(padx=2, pady=2)
            self.box.focus_set()
            self.box.pack(side=tk.TOP, padx=3, pady=3)
            self.buttn.pack(side=tk.TOP, padx=3, pady=3)
            self.result.pack(side=tk.TOP, padx=3, pady=3)
        def calc(self):
            word = self.box.get()
            while len(word) != 0:
                i = random.randrange(0, len(word))
                self.globvar.set(word[i])
                word = word[0:i] + word[i+1:]
    
    class MainApp(object):
        def __init__(self,master=None):
            self.master=master
            app=self.app=tk.Tk()
            app.title("Mkee's Tools")
            app.geometry('500x500')
            menubar=tk.Menu(app)
            self.shuffle=Shuffle(master)
            self.current=self.shuffle
            program_menu=tk.Menu(menubar)
            program_menu.add_command(label='Shuffle',command=self.show_shuffle)
            menubar.add_cascade(label='Programs', menu=program_menu)
            app.config(menu=menubar)
        def show_shuffle(self):
            self.current.hide()
            self.current=self.shuffle
            self.shuffle.show()
        def show_buttons(self):
            self.current.hide()
            self.current=self.buttons        
            self.buttons.show()
    
    def main():
        m=MainApp()
        m.app.mainloop()
    
    if __name__=='__main__':
        main()
    
    • 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'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

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.