Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104058

Re: exit from Tkinter mainloop Python 2.7

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: exit from Tkinter mainloop Python 2.7
Date 2016-03-04 23:28 +0100
Organization None
Message-ID <mailman.200.1457130500.20602.python-list@python.org> (permalink)
References <56e451a3-01f7-4883-b4db-c3a6a10729a9@googlegroups.com> <naimhq$3u4$1@dont-email.me> <539a3e8d-31eb-4199-9c05-03a89b2dab91@googlegroups.com>

Show all headers | View raw


kevind0718@gmail.com wrote:

> 
> 
> Christian & Others:
> 
> Thanks for your attention to this matter.
> My code now look like this:
> 
> from Tkinter import *
> 
> 
> def butContinue():
>     dbUser = entryName.get()

Here you set the local variable dbUser (every name you rebind inside a 
function is local to that function by default)

>     pWord =  entryPWord.get()
>     print dbUser

and here you print it.

>     print pWord
>     root1.quit()
> 
> 
> dbUser = ""

Here you set the global variable dbUser

> pWord  = ""
> root1 = Tk()
> ##root1.geometry("500x250")
> 
> 
> lblTop = Label(root1, text= '      Enter Values Below', font="Helvetica
> 14").grid(row=0, column=0, columnspan=2 , pady=5)
> ##lblTop.pack(side = TOP)
> 
> lblDB = Label(root1,    text= 'Weight').grid(row=1, column=0 )
> lblPWord = Label(root1, text= 'Height').grid(row=2,column=0)
> entryName = Entry(root1)
> entryName.grid(row=1, column=1, pady=5)
> 
> entryPWord = Entry(root1)
> entryPWord.grid(row=2, column=1, pady = 5)
> 
> butGo  =  Button(root1, text="   Continue "  , command=butContinue
> ).grid(row=3, column=1, sticky=W, pady=10)
> 
> 
> root1.mainloop()
> 
> print "After the MainLoop"
> print dbUser

and here you print that global variable.

> print pWord
> 
> After I type in the text Weight and Height
> No error are reported and output looks like this:
> 
> Weight
> Height
> After the MainLoop
> 
> 
> Question:
> Why did this code not cause Weight and Height to print again.
> 
> print "After the MainLoop"
> print dbUser
> print pWord
> 
> Thanks in advance.

If you want

> def butContinue():
>     dbUser = entryName.get()
>     pWord =  entryPWord.get()

to change the global dbUser and pWord you need to declare them as global 
explicitly:

> def butContinue():
      global dbUser, pWord
>     dbUser = entryName.get()
>     pWord =  entryPWord.get()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

exit from Tkinter mainloop  Python 2.7 kevind0718@gmail.com - 2016-02-23 13:39 -0800
  Re: exit from Tkinter mainloop Python 2.7 Christian Gollwitzer <auriocus@gmx.de> - 2016-02-23 23:29 +0100
    Re: exit from Tkinter mainloop Python 2.7 Peter Otten <__peter__@web.de> - 2016-02-23 23:45 +0100
  Re: exit from Tkinter mainloop Python 2.7 Christian Gollwitzer <auriocus@gmx.de> - 2016-02-23 23:33 +0100
    Re: exit from Tkinter mainloop Python 2.7 kevind0718@gmail.com - 2016-03-04 13:50 -0800
      Re: exit from Tkinter mainloop Python 2.7 Peter Otten <__peter__@web.de> - 2016-03-04 23:28 +0100
      Re: exit from Tkinter mainloop Python 2.7 Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-09 11:17 -0800
  Re: exit from Tkinter mainloop  Python 2.7 Dave Farrance <df@see.replyto.invalid> - 2016-02-24 09:34 +0000

csiph-web