Tkinter Confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MartinRinehart@gmail.com

    Tkinter Confusion

    Everything I've read about Tkinter says you create your window and
    then call its mainloop() method. But that's not really true. This is
    enough to launch a default window from the console:
    >>>from Tkinter import *
    >>>foo = Tk()
    Google's great, but it has no truth meter. Do I inherit from Frame? Or
    is that a big mistake. (Both positions repeated frequently.) Do I use
    Tk() or toplevel()? (Support for both and if a cogent explanation of
    the differences exists, I didn't find it.)

    Here's the application. I'm creating a visual parser for my beginner's
    language. The starting position is a list of Statement objects, each
    being a list of Token objects. The statement is presented as a list of
    buttons with abbreviated token types ('Con_Int' for a CONSTANT_INTEGE R
    token). Click the button and a dialog-like info display pops up with
    all the details about the token. During parsing, each recognition
    condenses tokens into productions, shortening the Statement. (Example:
    three Token buttons are replaced by one Addition production button.)
    An application window provides for stepping through the parsing and
    provides utility commands such as "Close all those token windows I've
    got lying all over".

    Much less complex than IDLE, but GvR and cohorts seem to understand
    what's really going on. I don't. Help appreciated.
  • Marc 'BlackJack' Rintsch

    #2
    Re: Tkinter Confusion

    On Sun, 17 Feb 2008 11:36:25 -0800, MartinRinehart wrote:
    Everything I've read about Tkinter says you create your window and
    then call its mainloop() method. But that's not really true. This is
    enough to launch a default window from the console:
    >
    >>>>from Tkinter import *
    >>>>foo = Tk()
    Depends on the platform if this shows a window.
    Do I use Tk() or toplevel()? (Support for both and if a cogent
    explanation of the differences exists, I didn't find it.)
    `Tk` is the main window, `Toplevel` for additional windows. Don't create
    several `Tk` instances. That usually causes very weird side effects.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • 7stud

      #3
      Re: Tkinter Confusion

      On Feb 17, 12:36 pm, MartinRineh...@ gmail.com wrote:
      Everything I've read about Tkinter says you create your window and
      then call its mainloop() method. But that's not really true. This is
      enough to launch a default window from the console:
      >
      >>from Tkinter import *
      >>foo = Tk()
      >
      You shouldn't care what happens in an interactive python session. In
      fact, you can take years off your life trying to figure out why the
      output of an interactive session is different from the output of a
      python program. Here is how to create a basic window with one widget:

      import Tkinter as tk

      root = tk.Tk()

      label = tk.Label(root, text='hello world')
      label.pack() #makes widget visible

      root.mainloop()


      Adding in some more details:


      import Tkinter as tk

      root = tk.Tk()
      root.geometry(' 600x400')
      root.config(bac kground='red')

      label = tk.Label(root, text='hello world', background='gra y')
      label.pack() #makes widget visible

      root.mainloop()


      Google's great, but it has no truth meter. Do I inherit from Frame?
      A frame is used to group widgets. You can have multiple frames each
      containing a group of widgets. That will allow you to place the group
      as a whole at a specific location in the window. Here's how to use
      frames to organize widgets:

      import Tkinter as tk

      root = tk.Tk()
      root.geometry(' 600x400')
      root.config(bac kground='red')

      frame1 = tk.Frame(root)
      label1 = tk.Label(frame1 , text='hello world', background='gra y')
      label2 = tk.Label(frame1 , text='goodbye', background='gra y')

      label1.pack() #makes label visible
      label2.pack() #makes label visible
      frame1.pack(sid e=tk.BOTTOM) #makes frame visible

      frame2 = tk.Frame(root)
      label3 = tk.Label(frame2 , text='yes', background='yel low')
      label4 = tk.Label(frame2 , text='no', background='yel low')
      label5 = tk.Label(frame2 , text='maybe', background='yel low')

      label3.pack()
      label4.pack()
      label5.pack()
      frame2.pack(sid e=tk.TOP)

      frame3 = tk.Frame(root)
      label6 = tk.Label(frame3 , text='a', background='blu e')
      label7 = tk.Label(frame3 , text='b', background='blu e')
      label8 = tk.Label(frame3 , text='c', background='blu e')

      label6.pack()
      label7.pack()
      label8.pack()
      frame3.pack(sid e=tk.LEFT)

      root.mainloop()

      Do I use
      Tk() or toplevel()? (Support for both and if a cogent explanation of
      the differences exists, I didn't find it.)
      >
      Tk() for you first window; Toplevel() for any additional windows you
      want to open:


      import Tkinter as tk

      root = tk.Tk()
      root.geometry(' 300x200+50+50') #+x+y positions window
      root.config(bac kground='red')

      label = tk.Label(root, text='hello world', background='gra y')
      label.pack()

      window2 = tk.Toplevel()
      window2.geometr y('300x200+400+ 50')

      root.mainloop()



      Comment

      • 7stud

        #4
        Re: Tkinter Confusion

        MartinRineh...@ gmail.com wrote:
        Do I use
        Tk() or toplevel()? (Support for both and if a cogent explanation of
        the differences exists, I didn't find it.)
        >
        If you close the window created by Tk(), the program terminates. If
        you close a window created by Toplevel() only that window closes. The
        Tk() window remains open and the program continues to execute.

        Comment

        • MartinRinehart@gmail.com

          #5
          Re: Tkinter Confusion

          Many thanks to all.

          Comment

          Working...