Jump to content

Tkinter question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
Hi guys. I'm pretty new to programming, although I have been tinkering with Python for just over a year. To help me learn and get used to making menus, I decided to make a simple math program that solves for area of several shapes. The menu part of the script reads:

root = Tk()

menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Circle", command=circle)
filemenu.add_command(label="Circumference", command=circum)
filemenu.add_separator()
filemenu.add_command(label="Parallelogram", command=parall)
filemenu.add_command(label="Rectangle", command=rectangle)
filemenu.add_separator()
filemenu.add_command(label="Trapezoid", command=trapezoid)
filemenu.add_command(label="Triangle", command=triangle)
filemenu.add_command(label="Exit", command=callback)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback)

mainloop()

==============================================
I know that I am missing something at the end, but I'm unsure what. IDLE tells me 'Token Error: EOF in mult-line statement'. I don't really know what that means but how do I fix it? Also, a few random questions. Can you use Py2exe and/or Pyinstaller to convert .pyw to exe's? How would I make a console-like screen under the menu (also how to size window)? And lastly, how could I get python to open a web link? I would like to have an Online button on another program that would open the official site in the default browser. Thanks in advance!

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It's usually easier, and cleaner to make a widget, or multiple, when working with Tkinter. It actually suprises me a bit, that you aren't using them after an year with Tkinter.

You're using the functions the wrong times. You should configure root, when all the menu-stuff is done. And you should first add the cascade when each popup-menu is finished. Take a look at this code:
from Tkinter import *

root = Tk()

def foo():
    print "foobarbaz"

menu = Menu(root)

filemenu = Menu(menu, tearoff=0)
filemenu.add_command(label="Circle", command=foo)
filemenu.add_command(label="Circumference", command=foo)
filemenu.add_separator()
filemenu.add_command(label="Parallelogram", command=foo)
filemenu.add_command(label="Rectangle", command=foo)
filemenu.add_separator()
filemenu.add_command(label="Trapezoid", command=foo)
filemenu.add_command(label="Triangle", command=foo)
filemenu.add_command(label="Exit", command=foo)
menu.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menu, tearoff=0)
helpmenu.add_command(label="About...", command=foo)
menu.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menu)
root.mainloop()
Note, in the Menu()-constructor, I'm setting an option, tearoff. It means you can't move the popup menu in the application, it'll be stuck where it was created.

Victor said:

Can you use Py2exe and/or Pyinstaller to convert .pyw to exe's?
Yes, it's possible.
The executable-files, will be very slow, because the executable-file have to include the whole Python run-time library, so it isn't flexible

I can't help you with the rest, sorry.

#3
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
thanks a bunch!