Re: exit from Tkinter mainloop Python 2.7
On Friday, March 4, 2016 at 3:50:43 PM UTC-6, kevin...@gmail.com wrote: > Thanks for your attention to this matter. > My code now look like this: [...] All of the help offered in this thread ignores the elephant in the room. What are you trying to achieve here Kevin? To me, this looks like some sort of dialog, but if it is a dialog, the whole design is a miserable failure. And why in the hell would you call the w.quit() method when the user presses a "continue" button? Do you realize that the quit method does not "close the dialog" (if you can call this a dialog), no, but simply instructs Tkinter to stop processing events? Once a user clicks your "continue" button, he can no longer interact with the GUI, and the user is left in a state of befuddlement. Those who've attempted to help you have suggested that you utilize the quit method in your "continue button callback" so that the widgets will stay alive long enough for you to fetch the value, but that is horrible advice. Trust me, the quit method is not something you want to use unless you know what you're doing. It's should never be used in daily coding, and only in very specific advanced circumstances. The destroy method that you utilized initially *IS* the correct course of action here. You see, your interface is not a failure because of the destroy method, no, it is a failure because you failed to (1) fetch the values in the button callback BEFORE the widgets are destroyed, and (2) You failed to store those values somewhere that will be persistent after the function returns. But even if you followed both of these suggestions, you design is flawed. If you want to create a proper "dialog interface", the key is not to use the "quit" method, but to follow these simple design rules: (1) Present the user with a "focused window" that contains "form" filled with "input fields" that will facilitate the inputting of the required values. Typically, but not always, you'll want this window to block program execution until the user submits the form (FYI: Dialogs that block are commonly referred to as "modal dialogs") (2) Include at least one button to submit the values. This button is typically named "Okay", or "Yes" or "Submit". But when the user presses this button, the dialog will *NOT* be closed until the values have been validated. If the values are found to be inadequate, the dialog will inform the user of his mistakes and refocus the input-form for editing. A more advanced dialog will utilize fields that validate their *OWN* values in "real time". These "real time validations" are less likely to confuse a user, and they facilitate a more efficient interface since the user will be aware of mistakes as they occur. (3) Include at least one button to cancel the interaction. Sometimes, a user will open a dialog accidentally, or for some other reason decide that they need a way out. For instance: Imagine all the times that you've accidentally pressed the X button on a window. If the window closed without first asking you "Do you really want to close without saving changes", you'd be screwed! A proper "dialog window" will be constructed of three basic top-level components: +--+ |DIALOG_TITLE | +--+ | | | DIALOG_BODY| | | +--+ | DIALOG_BUTTONS | +--+ The DIALOG_TITLE is simply a string that will be displayed in the title bar of the dialog. The DIALOG_BODY is a fluid area in the middle that can be filled with user-editable fields, and the DIALOG_BUTTONS is an area at the bottom where appropriate buttons will be displayed. A proper dialog object will allow the caller to display the dialog in either "modal" or "non-modal" forms, and it will expose hooks that will allow the programmer to customize the validation and submission of the form data. An even more advanced version will require the programmer to extend a "form object" and pass it into the constructor. Fortunately, the tkSimpleDialog has already wrapped *SOME* of this functionality up for you. All you need to do is import the class and override a few "virtual methods" to achieve your desired goal. The tkSimpleDialod.Dialog class exposes 3 hooks that you can utilize to modify the behavior (1) body: In this method you create your widgets.It requires one argument which is a tk.Frame instance that the widgets can be made a child of. (2) validate: in this method you validate your widgets and return a Boolean flag. If you don't need any validation then don't bother to override it. (3) apply: In this method you can do something with the "dialog.result" *AFTER* the dialog closes. Typically though, you will handle this action from outside the dialog object. But in certain limited circumstances, it can si
Re: exit from Tkinter mainloop Python 2.7
kevind0...@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() -- https://mail.python.org/mailman/listinfo/python-list
Re: exit from Tkinter mainloop Python 2.7
Christian & Others: Thanks for your attention to this matter. My code now look like this: from Tkinter import * def butContinue(): dbUser = entryName.get() pWord = entryPWord.get() print dbUser print pWord root1.quit() 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 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. KBD -- https://mail.python.org/mailman/listinfo/python-list
Re: exit from Tkinter mainloop Python 2.7
kevind0...@gmail.com wrote: >from Tkinter import * > >def butContinue(): >root1.destroy() As Christian said, you're destroying the root window and its children, so instead use root1.quit() here. > ... > >root1.mainloop() > >print entryName.get("1.0", "end-1c" ) >print entryPWord.get("1.0", "end-1c" ) And your root1.destroy() goes here instead. (The root window would normally be destroyed on the script exit, but some IDE debuggers will leave it open.) -- https://mail.python.org/mailman/listinfo/python-list
Re: exit from Tkinter mainloop Python 2.7
Christian Gollwitzer wrote: > Am 23.02.16 um 22:39 schrieb kevind0...@gmail.com: >> from Tkinter import * >> >> def butContinue(): >> root1.destroy() >> [...] >> entryName = Entry(root1).grid(row=1, column=1, pady=5) >> [...] >> butGo = Button(root1, text=" Continue " , command=butContinue >> ).grid(row=3, column=1, sticky=W, pady=10) >> >> root1.mainloop() >> >> print entryName.get("1.0", "end-1c" ) > >> When I click on butGo I get the error below. >> So is this some sort of scope error? >> why does entryName not exist for me to grab it's value? > > You call destroy() on the root window of Tk. If you destroy a window, > that will destroy all of it's children. Therefore by deleting the root > window, also the entryName widget was deleted. You need to export the > values before you close the window, i.e. in the butContinue() function. Even when you follow this advice, entryName was set to None by the line >> entryName = Entry(root1).grid(row=1, column=1, pady=5) as grid() always returns None. You need two steps entryName = Entry(root1) entryName.grid(row=1, column=1, pady=5) Also, >> print entryName.get("1.0", "end-1c" ) I believe that the Entry widget's get() method doesn't take any arguments. -- https://mail.python.org/mailman/listinfo/python-list
Re: exit from Tkinter mainloop Python 2.7
Am 23.02.16 um 22:39 schrieb kevind0...@gmail.com: 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) Also here, the labels look odd. Have you tried to do some alignment of the text with spaces? Remove the sapces and look at the "justify" option of the label widget and the "sticky" option for grid. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: exit from Tkinter mainloop Python 2.7
Am 23.02.16 um 22:39 schrieb kevind0...@gmail.com: from Tkinter import * def butContinue(): root1.destroy() [...] entryName = Entry(root1).grid(row=1, column=1, pady=5) [...] butGo = Button(root1, text=" Continue " , command=butContinue ).grid(row=3, column=1, sticky=W, pady=10) root1.mainloop() print entryName.get("1.0", "end-1c" ) When I click on butGo I get the error below. So is this some sort of scope error? why does entryName not exist for me to grab it's value? You call destroy() on the root window of Tk. If you destroy a window, that will destroy all of it's children. Therefore by deleting the root window, also the entryName widget was deleted. You need to export the values before you close the window, i.e. in the butContinue() function. There is another pitfall with Tk: if you delete the main window, you can get problems if you try to recreate it. If you want to show several pages in sequence or the like, you should withdraw the main window root1.withdraw() and popup a fresh toplevel by t=Toplevel() and do everything in t. Once you are finished, destroying t will keep your application alive (because the real root window is just hidden) Another comment: > root1.geometry("500x250") Do not do this. The grid and pack geometry managers compute the needed space automatically. A fixed size in pixels can easily break, when you go to another computer with a different font size, screen resolution, or a different OS. If you are not satisfied with the whitespace, use the padding options of grid or pack. Christian -- https://mail.python.org/mailman/listinfo/python-list
exit from Tkinter mainloop Python 2.7
Hello: Newbie here. Spent the a good part of the day tinkering and reading tutorials, I was able to create a sample that is very close to my requirement. When I execute the code below the Dialog displayed as expected and I can enter data into the textboxes. All good. When I click on butGo I get the error below. So is this some sort of scope error? why does entryName not exist for me to grab it's value? Your kind assistance is requested. Traceback (most recent call last): File "C:\Users\kduffy12\workspace\testPythonA\testA\simpleDialog.py", line 25, in print entryName.get("1.0", "end-1c" ) AttributeError: 'NoneType' object has no attribute 'get' from Tkinter import * def butContinue(): root1.destroy() 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).grid(row=1, column=1, pady=5) entryPWord = Entry(root1).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 entryName.get("1.0", "end-1c" ) print entryPWord.get("1.0", "end-1c" ) -- https://mail.python.org/mailman/listinfo/python-list