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


Groups > comp.lang.python > #21069

Re: Is it necessary to call Tk() when writing a GUI app with Tkinter?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Is it necessary to call Tk() when writing a GUI app with Tkinter?
Date 2012-03-01 00:24 -0500
References <27603449.17.1330492001624.JavaMail.geo-discussion-forums@vbbfv2> <6e1c521e-14be-4ec0-9ff0-7f23fd9cd3dc@f14g2000yqe.googlegroups.com> <mailman.306.1330561055.3037.python-list@python.org> <c556fe7d-3f45-4479-8b81-cc0c25af41a2@f2g2000yqh.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.313.1330579461.3037.python-list@python.org> (permalink)

Show all headers | View raw


On 2/29/2012 10:22 PM, Rick Johnson wrote:
I do not know what book the OP is referring to,
but the current doc example is
http://docs.python.org/py3k/library/tkinter.html#a-simple-hello-world-program
My current replacement (see below) can be downloaded from the tracker:
http://bugs.python.org/issue14163

> If you want to keep things simple, i would:
>
>   1. Create the root window explicitly!

It already does that.

>   2. Bind the command of the button to root.destroy
> (command=root.destroy)

That works great. My current replacement example is uploaded to the 
issue http://bugs.python.org/issue14163

> PS: I would highly suggest against using the "from Tkinter import *".
> Instead, use "import Tkinter as tk" and prefix all module contents
> with "tk.".

I have changed the example to do that. I also showed the alternate to 
initialize a widget. Here is the current version, tested on Windows 3.2.2.

import tkinter as tk

class Application(tk.Frame):
     def __init__(self, master=None):
         tk.Frame.__init__(self, master)
         self.pack()
         self.createWidgets()

     def createWidgets(self):
         self.hi_there = tk.Button(self)
         self.hi_there["text"] = "Hello_World\n(click_me)",
         self.hi_there["command"] = self.say_hi
         self.hi_there.pack({"side": "top"})

         self.QUIT = tk.Button(self, text = "QUIT", fg = "red", command 
= root.destroy)
         self.QUIT.pack({"side": "bottom"})

     def say_hi(self):
         print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

There is a minor problem left. The hi_there Button text has underscores 
because if I use spaces instead, tk surrounds the text with {bra ces}. 
This seems bizarre. Is there any way to have Button text with spaces and 
no braces?

-- 
Terry Jan Reedy

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


Thread

Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-28 21:06 -0800
  Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-29 06:24 -0800
    Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-02-29 19:17 -0500
      Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-29 19:22 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-01 00:24 -0500
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:49 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 19:02 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-01 22:43 -0500
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-02 15:19 -0500
              Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Mel Wilson <mwilson@the-wire.com> - 2012-03-02 15:52 -0500
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? "Russell E. Owen" <rowen@uw.edu> - 2012-03-05 11:45 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 21:40 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:53 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Mark Roseman <mark@markroseman.com> - 2012-03-02 10:57 -0700
              Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-02 10:53 -0800
    Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 20:41 -0800
      Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Terry Reedy <tjreedy@udel.edu> - 2012-03-01 00:40 -0500
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 21:45 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:14 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:55 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:14 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 21:45 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:41 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 22:41 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-01 07:38 +0000
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-02-29 23:58 -0800
      Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 18:35 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-01 19:15 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-01 19:19 -0800
            Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-01 21:22 -0800
        Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? John Salerno <johnjsal@gmail.com> - 2012-03-02 14:16 -0800
          Re: Is it necessary to call Tk() when writing a GUI app with Tkinter? Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-03 20:27 -0800

csiph-web