473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1532
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

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

Similar topics

0
by: |-|erc | last post by:
Hi! Small challenge for you. The index.php uses this file and calls layout(). Take a look at www.chatty.net this file draws the chat login box on the right. I traced the CHAT button it submits...
5
by: john | last post by:
I searched http://www.sellsbrothers.com. and could not find anything about this subject. How do I make C# User Controls Visible to Visual Basic 6.0 Applications? Thanks, John
3
by: Rajeev | last post by:
I am writing an Attendance Management system in C#.NET. The data which comes from the table looks like this: PunchTime PunchType 5/13/2004 9:00:00 In 5/13/2004 9:15:00 ...
2
by: Dennis | last post by:
I have a control that inheirts from "Panel". I want to prevent the user from having access to the MoueUp event. I tried making a sub in my control that handles MyBase.MouseUp but still this event...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
5
by: ddhung | last post by:
how to insert multible values by making one selection from combo box in forms. any way to code insert SQL in froms??
18
by: dbahooker | last post by:
team i'm having a tough time getting these data readers to work correctly I'd just like to be able to centralize my GetDataReader functions; so i can pass a simple SQL statement and be passed...
1
by: Doug | last post by:
What is the simplest way to make a report where only the records where a field matches a certain date are included, and the user first selects that date (from form or popup)? (I can write the SQL...
0
by: onegative | last post by:
G'day Y'all, I was hoping to get some expert feedback on a proposal I am considering regarding a new internal application to help fill some gaps in our IT department. I have some configuration...
8
by: sheldoncs | last post by:
System.InvalidOperationException was unhandled Message="Client found response content type of 'text/plain; charset=utf-8', but expected 'text/xml'. The request failed with the error message: --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.