473,574 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making Forms

60 New Member
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 1545
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Expert
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
psychofish25
60 New Member
... 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.gr id(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.CreateWidg ets()"
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 Recognized Expert Expert
Thank you for your help, however, I am a bit confused.

-What do you mean by SUB CLASS the widget?
-In selectButton.gr id(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.CreateWidg ets()"
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 Recognized Expert Expert
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 Recognized Expert Expert
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
psychofish25
60 New Member
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 Recognized Expert Moderator Specialist
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
psychofish25
60 New Member
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

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

Similar topics

0
2756
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 and goes to the index file again, I can't figure out how it opens the chatroom. I want to get it to skip the login box and go straight to the...
5
524
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
1704
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 Out 5/13/2004 10:00:34 In 5/13/2004 11:08:00 Out I can never be sure how many times a person will punch in or out in a
2
1305
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 shows up as an event the user can use. Is there anyway to prevent users of this control from getting access to events such as MouseUp. Thanks....
90
4360
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 immutable instances? -- \ "Love is the triumph of imagination over intelligence." -- |
5
1405
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
1732
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 back a valid DataReader object. I've been slaving with this all day long. I've been trying 'new' and not new and just slaving over this; im...
1
5582
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 to select the records I want...)
0
1523
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 data that would work well (very well in fact) defined as XML documents and I want to enrich that data with additional User defined information (child...
8
4036
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: -- System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.UnauthorizedAccessException: Access to the path...
0
7841
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7758
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7856
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5651
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5335
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.