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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:47:25+00:00 2026-05-23T12:47:25+00:00

I’m trying to create a GUI in Python using the Tkinter module, and part

  • 0

I’m trying to create a GUI in Python using the Tkinter module, and part of that involves giving the user the option of two radioboxes, and to select which one they want. Depending on which box they tick, it then runs different functions which return different results – results that I then want to use outside of the window class. But I don’t know how to send a value from inside the class to outside the class; I’m sure its fairly simple but I can’t for the life of me work it out.

My current code is:

class BatchIndiv():
def __init__(self, master):
    self.master=master
    self.startwindow()
    self.b=0

def startwindow(self):

    self.var1 = IntVar()
    self.textvar = StringVar()

    self.Label1=Label(self.master, text="Batch or indivdual import?")
    self.Label1.grid(row=0, column=0)

    self.Label2=Label(self.master, textvariable=self.textvar)
    self.Label2.grid(row=2, column=0)

    self.rb1 = Radiobutton(self.master, text="Batch", variable=self.var1,
                           value=1, command=self.cb1select)
    self.rb1.grid(row=1, column=0, sticky=W)

    self.rb2 = Radiobutton(self.master, text="Individual", variable=self.var1,
                           value=2, command=self.cb1select)
    self.rb2.grid(row=1, column=1, sticky=W)

    self.Button1=Button(self.master, text="ok", command=self.ButtonClick)
    self.Button1.grid(row=1, column=2)

def ButtonClick(self):
     if (self.var1.get())==1:
        b=BatchImport()
        return b
        self.master.quit()
        self.master.destroy()
     elif (self.var1.get())==2:
        b=IndivImport()
        return b
        self.master.quit()
        self.master.destroy()
     else: pass

def cb1select(self):
    return self.var1.get()

#End of class definition.
#Code:

root=Tk()
window=BatchIndiv(root)
b=BatchIndiv.ButtonClick.b
root.mainloop()

....

Treat the BatchImport and IndivImport functions as black boxes, they just return an integer value, which I assign to the variable b inside ButtonClick(). I need that value to do some stuff below root.mainloop(), (i.e. where …. is), but I don’t know how to get it. Tkinter is really quite irritating, especially as everyone has different methods of doing things so the online documentation is never the same – tried doing what was written in various ones and it just gave me more lovely error messages.

Any and all help would be appreciated.

PS – how can I make the window close when the button is pressed, and still send the value b back to the rest of the code, and not just quit python completely? As you can see I tried using .quit() and .destroy() but to no luck.

  • 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-23T12:47:25+00:00Added an answer on May 23, 2026 at 12:47 pm

    Your variable b is just local to your class, so it the moment your class is deleted (after you do destroy or quit), b gets destroyed. So define the variable b as global.

    b = 0                    # this is now in the global namespace
    
    class BatchIndiv():
        def __init__(self, master):
            self.master=master
            self.startwindow()
            #self.b=0      # no need for this, directly store in the global variable
    
        def startwindow(self):
    
            self.var1 = IntVar()
            self.textvar = StringVar()
    
            self.Label1=Label(self.master, text="Batch or indivdual import?")
            self.Label1.grid(row=0, column=0)
    
            self.Label2=Label(self.master, textvariable=self.textvar)
            self.Label2.grid(row=2, column=0)
    
            self.rb1 = Radiobutton(self.master, text="Batch", variable=self.var1,
                                   value=1, command=self.cb1select)
            self.rb1.grid(row=1, column=0, sticky=W)
    
            self.rb2 = Radiobutton(self.master, text="Individual", variable=self.var1,
                                   value=2, command=self.cb1select)
            self.rb2.grid(row=1, column=1, sticky=W)
    
            self.Button1=Button(self.master, text="ok", command=self.ButtonClick)
            self.Button1.grid(row=1, column=2)
    
        def ButtonClick(self):
            global b
            if (self.var1.get())==1:
                b=BatchImport()
                self.master.quit()
                #self.master.destroy()    # either quit or destroy, I think one is sufficient, but confirm to be sure.
            elif (self.var1.get())==2:
                b=IndivImport()
                self.master.quit()
                #self.master.destroy()    # either quit or destroy, I think one is sufficient, but confirm to be sure
             else: pass
    
        def cb1select(self):
            return self.var1.get()
    
    #End of class definition.
    #Code:
    
    root=Tk()
    window=BatchIndiv(root)
    root.mainloop()
    
    # now do here whatever you want to do with the variable b
    print b
    

    (using global variable is not a good idea, but since I don’t know what you want to do with b , I am not able to suggest anything.)

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

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.