471,305 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Making Forms

I am new to python and I was wondering if I could make a form for my code. I would start out by just making a window with a Button and a Label that displays "Hello World!" when I click on the button. Is this possible? If not could I make a form with another language, such as VB.NET and use that?

Thank you for your help,
Jordan
Jul 16 '07 #1
12 1415
bartonc
6,596 Expert 4TB
I am new to python and I was wondering if I could make a form for my code. I would start out by just making a window with a Button and a Label that displays "Hello World!" when I click on the button. Is this possible? If not could I make a form with another language, such as VB.NET and use that?

Thank you for your help,
Jordan
It is far easier in Python than any other programming language that I have used.
I'll whip up an example for you and post it here.

Back in a jiffy...
Jul 16 '07 #2
bartonc
6,596 Expert 4TB
It is far easier in Python than any other programming language that I have used.
I'll whip up an example for you and post it here.

Back in a jiffy...
... had to cook dinner... Here it is:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkSimpleDialog
  3.  
  4. ## The trick is to SUB CLASS the widget that you want to add fuctionality to. ##
  5. ## A Tkinter.Frame is just a blank frame until you subclass it an give it something
  6. ## to do ##  HAVE FUN
  7.  
  8. class MyFrame(Frame):
  9.     """A subclass of Tkinter.Frame. This is the best way to make a "window"."""
  10.     def __init__(self, parent, *args, **kwargs):
  11.         Frame.__init__(self, parent, *args, **kwargs)
  12.  
  13.         self.CreateWidgets()
  14.  
  15.  
  16.     def CreateWidgets(self):
  17.         """Document your widget creation here."""
  18.         # Here is an entry
  19.         self.nameEntry = Entry(self)
  20.         self.nameEntry.grid(row=0, column=0, columnspan=3)
  21.  
  22.         # Here is a button 'bound' to an event. #
  23.         selectButton = Button(self, text='Enter')
  24.         selectButton.grid(row=2, column=1, rowspan=2)
  25.         selectButton.bind("<Button-1>", self.OnButton)
  26.  
  27.  
  28.     def OnButton(self, event):
  29.         """Document your button action here:
  30.             Just print the contents of the entry widget."""
  31.         text = self.nameEntry.get()
  32.         if not text:
  33.             dialog = tkSimpleDialog.Dialog(self, "Empty text entry!")
  34.         else:
  35.             dialog = tkSimpleDialog.Dialog(self, text)
  36.         dialog.destroy()
  37.  
  38.  
  39.  
  40. if __name__ == "__main__":
  41.     root = Tk()
  42.  
  43.     frame = MyFrame(root)
  44.     # One of the hardest parts is getting the layout to cooperate.
  45.     frame.grid(row=0, column=0)
  46.  
  47.  
  48.     root.mainloop()
  49.  
  50.  
Jul 16 '07 #3
... had to cook dinner... Here it is:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkSimpleDialog
  3.  
  4. ## The trick is to SUB CLASS the widget that you want to add fuctionality to. ##
  5. ## A Tkinter.Frame is just a blank frame until you subclass it an give it something
  6. ## to do ##  HAVE FUN
  7.  
  8. class MyFrame(Frame):
  9.     """A subclass of Tkinter.Frame. This is the best way to make a "window"."""
  10.     def __init__(self, parent, *args, **kwargs):
  11.         Frame.__init__(self, parent, *args, **kwargs)
  12.  
  13.         self.CreateWidgets()
  14.  
  15.  
  16.     def CreateWidgets(self):
  17.         """Document your widget creation here."""
  18.         # Here is an entry
  19.         self.nameEntry = Entry(self)
  20.         self.nameEntry.grid(row=0, column=0, columnspan=3)
  21.  
  22.         # Here is a button 'bound' to an event. #
  23.         selectButton = Button(self, text='Enter')
  24.         selectButton.grid(row=2, column=1, rowspan=2)
  25.         selectButton.bind("<Button-1>", self.OnButton)
  26.  
  27.  
  28.     def OnButton(self, event):
  29.         """Document your button action here:
  30.             Just print the contents of the entry widget."""
  31.         text = self.nameEntry.get()
  32.         if not text:
  33.             dialog = tkSimpleDialog.Dialog(self, "Empty text entry!")
  34.         else:
  35.             dialog = tkSimpleDialog.Dialog(self, text)
  36.         dialog.destroy()
  37.  
  38.  
  39.  
  40. if __name__ == "__main__":
  41.     root = Tk()
  42.  
  43.     frame = MyFrame(root)
  44.     # One of the hardest parts is getting the layout to cooperate.
  45.     frame.grid(row=0, column=0)
  46.  
  47.  
  48.     root.mainloop()
  49.  
  50.  
Thank you for your help, however, I am a bit confused.

-What do you mean by SUB CLASS the widget?
-In selectButton.grid(row=2, column=1, rowspan=2), what do those parameters mean?
-What is tkSimpleDialog?
-" def __init__(self, parent, *args, **kwargs):
Frame.__init__(self, parent, *args, **kwargs)

self.CreateWidgets()"
What does this do?
-Is there a form designer I can download where I can draw the buttons and labels, etc. Instead of typing this code because it confuses me.
Jul 16 '07 #4
bartonc
6,596 Expert 4TB
Thank you for your help, however, I am a bit confused.

-What do you mean by SUB CLASS the widget?
-In selectButton.grid(row=2, column=1, rowspan=2), what do those parameters mean?
-What is tkSimpleDialog?
-" def __init__(self, parent, *args, **kwargs):
Frame.__init__(self, parent, *args, **kwargs)

self.CreateWidgets()"
What does this do?
GUI frameworks extend the Python's idea of Object Oriented Programming. In order to understand any framework, you'll need to get the basics of classes under your belt first.

-Is there a form designer I can download where I can draw the buttons and labels, etc. Instead of typing this code because it confuses me.
There are many GUI toolkits available. Here is the most helpful like to Find Your GUI Toolkit for Python that I have found.
Jul 16 '07 #5
bartonc
6,596 Expert 4TB
GUI frameworks extend the Python's idea of Object Oriented Programming. In order to understand any framework, you'll need to get the basics of classes under your belt first.


There are many GUI toolkits available. Here is the most helpful like to Find Your GUI Toolkit for Python that I have found.
I just found a very nice intro to Python in PDF form, written by Python's founder:
Introduction to Python by Guido van Rossum. I've also seen the first edition of "Learning Python" (the second edition is my favorite beginner's handbook) on line in PDF form.
Jul 16 '07 #6
bartonc
6,596 Expert 4TB
There are many GUI toolkits available. Here is the most helpful like to Find Your GUI Toolkit for Python that I have found.
If you are a Visual Studio user, there's VisualWx which gives a similar look and feel.
Jul 16 '07 #7
Thank you for your help. I looked over Object Oriented Programming with Python and it helped a lot. Now I understand the code much better. I just have one question:

What does the If __name__ = "__main__": statement do?
If you think it's more appropriate for me to start a new thread I shall do so and you are free to close this thread.
Jul 17 '07 #8
bvdet
2,851 Expert Mod 2GB
Thank you for your help. I looked over Object Oriented Programming with Python and it helped a lot. Now I understand the code much better. I just have one question:

What does the If __name__ = "__main__": statement do?
If you think it's more appropriate for me to start a new thread I shall do so and you are free to close this thread.
I answered the same question in another thread. Maybe we should post an article.

A module type is a container that holds objects loaded with the import statement. The identifier __name__ is assigned to the module name. If the module is run as a top level program, __name__ is assigned to the string '__main__'.

Expand|Select|Wrap|Line Numbers
  1. if __name__ == '__main__':
  2.     .... execute code ....
  3.  
The code under the 'if' statement only executes if the module is run as a program. That way the functions defined above can be imported by other programs without executing the conditional code.
Jul 17 '07 #9
I answered the same question in another thread. Maybe we should post an article.

A module type is a container that holds objects loaded with the import statement. The identifier __name__ is assigned to the module name. If the module is run as a top level program, __name__ is assigned to the string '__main__'.

Expand|Select|Wrap|Line Numbers
  1. if __name__ == '__main__':
  2.     .... execute code ....
  3.  
The code under the 'if' statement only executes if the module is run as a program. That way the functions defined above can be imported by other programs without executing the conditional code.
Okay, thank you very much for giving me a quick and thorough answer.
Jul 17 '07 #10
I have tried numerous times to understand the code provided to me about how to draw forms. I have read over framework and OOP. I just still have a few questions that need to be answered:

What are all those parameters after __init__ and what do they do?
[def __init__(self, parent, *args, **kwargs):]

What is an entry?

Where do you specify the forms width and height?

Where do you specify where to draw the button?
Jul 30 '07 #11
bartonc
6,596 Expert 4TB
I have tried numerous times to understand the code provided to me about how to draw forms. I have read over framework and OOP. I just still have a few questions that need to be answered:

What are all those parameters after __init__ and what do they do?
[def __init__(self, parent, *args, **kwargs):]

What is an entry?

Where do you specify the forms width and height?

Where do you specify where to draw the button?
Because of the way Tkinter defines it's object constructors, we copy that style in our subclass:
Expand|Select|Wrap|Line Numbers
  1. #
  2.     def __init__(self, parent, *args, **kwargs):
  3.         Frame.__init__(self, parent, *args, **kwargs)
can best be explained by Mark Lutz. He calls it "varargs" on p 338 of
7. More argument matching examples. Here is the sort of interaction you should get, along with comments that explain the matching that goes on:
Expand|Select|Wrap|Line Numbers
  1. def f1(a, b): print a, b             # normal args
  2.  
  3. def f2(a, *b): print a, b            # positional varargs
  4.  
  5.  f3(a, **b): print a, b           # keyword varargs
  6.  
  7.  f4(a, *b, **c): print a, b, c    # mixed modes
  8.  
  9.  f5(a, b=2, c=3): print a, b, c   # defaults
  10.  
  11.  f6(a, b=2, *c): print a, b, c    # defaults + positional varargs
  12. % python
  13. >>> f1(1, 2)                  # matched by position (order matters)
  14. 1 2
  15. >>> f1(b=2, a=1)              # matched by name (order doesn't matter)
  16. 1 2
  17. >>> f2(1, 2, 3)               # extra positionals collected in a tuple
  18. 1 (2, 3)
  19. >>> f3(1, x=2, y=3)           # extra keywords collected in a dictionary
  20. 1 {'x': 2, 'y': 3}
  21. >>> f4(1, 2, 3, x=2, y=3)     # extra of both kinds
  22. 1 (2, 3) {'x': 2, 'y': 3}
Entries are text controls (called TextCtrl in wxPython).

Height and width are usually dynamic in Tkinter. They are set by the Grid() or Pack() geometry management functions. Those function (which should never be mixed together) are also responsible for the placement of all widgets on a window.

Buttons are created just as any other object in Tkinter, then its .grid() (or .pack() method is called to place it.

In general, Tkinter is very clunky. It is good for beginners to get the feel of object oriented programming in a graphical user interface environment, but (I feel) its usefulness end there.
Jul 30 '07 #12
Okay, that answers my question and thus the thread dies.
Jul 30 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by |-|erc | last post: by
3 posts views Thread by Rajeev | last post: by
90 posts views Thread by Ben Finney | last post: by
18 posts views Thread by dbahooker | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.