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
12 1532
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...
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: - from Tkinter import *
-
import tkSimpleDialog
-
-
## The trick is to SUB CLASS the widget that you want to add fuctionality to. ##
-
## A Tkinter.Frame is just a blank frame until you subclass it an give it something
-
## to do ## HAVE FUN
-
-
class MyFrame(Frame):
-
"""A subclass of Tkinter.Frame. This is the best way to make a "window"."""
-
def __init__(self, parent, *args, **kwargs):
-
Frame.__init__(self, parent, *args, **kwargs)
-
-
self.CreateWidgets()
-
-
-
def CreateWidgets(self):
-
"""Document your widget creation here."""
-
# Here is an entry
-
self.nameEntry = Entry(self)
-
self.nameEntry.grid(row=0, column=0, columnspan=3)
-
-
# Here is a button 'bound' to an event. #
-
selectButton = Button(self, text='Enter')
-
selectButton.grid(row=2, column=1, rowspan=2)
-
selectButton.bind("<Button-1>", self.OnButton)
-
-
-
def OnButton(self, event):
-
"""Document your button action here:
-
Just print the contents of the entry widget."""
-
text = self.nameEntry.get()
-
if not text:
-
dialog = tkSimpleDialog.Dialog(self, "Empty text entry!")
-
else:
-
dialog = tkSimpleDialog.Dialog(self, text)
-
dialog.destroy()
-
-
-
-
if __name__ == "__main__":
-
root = Tk()
-
-
frame = MyFrame(root)
-
# One of the hardest parts is getting the layout to cooperate.
-
frame.grid(row=0, column=0)
-
-
-
root.mainloop()
-
-
... had to cook dinner... Here it is: - from Tkinter import *
-
import tkSimpleDialog
-
-
## The trick is to SUB CLASS the widget that you want to add fuctionality to. ##
-
## A Tkinter.Frame is just a blank frame until you subclass it an give it something
-
## to do ## HAVE FUN
-
-
class MyFrame(Frame):
-
"""A subclass of Tkinter.Frame. This is the best way to make a "window"."""
-
def __init__(self, parent, *args, **kwargs):
-
Frame.__init__(self, parent, *args, **kwargs)
-
-
self.CreateWidgets()
-
-
-
def CreateWidgets(self):
-
"""Document your widget creation here."""
-
# Here is an entry
-
self.nameEntry = Entry(self)
-
self.nameEntry.grid(row=0, column=0, columnspan=3)
-
-
# Here is a button 'bound' to an event. #
-
selectButton = Button(self, text='Enter')
-
selectButton.grid(row=2, column=1, rowspan=2)
-
selectButton.bind("<Button-1>", self.OnButton)
-
-
-
def OnButton(self, event):
-
"""Document your button action here:
-
Just print the contents of the entry widget."""
-
text = self.nameEntry.get()
-
if not text:
-
dialog = tkSimpleDialog.Dialog(self, "Empty text entry!")
-
else:
-
dialog = tkSimpleDialog.Dialog(self, text)
-
dialog.destroy()
-
-
-
-
if __name__ == "__main__":
-
root = Tk()
-
-
frame = MyFrame(root)
-
# One of the hardest parts is getting the layout to cooperate.
-
frame.grid(row=0, column=0)
-
-
-
root.mainloop()
-
-
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.
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.
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.
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.
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.
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__'. - if __name__ == '__main__':
-
.... execute code ....
-
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.
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__'. - if __name__ == '__main__':
-
.... execute code ....
-
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.
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?
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: - #
-
def __init__(self, parent, *args, **kwargs):
-
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: -
def f1(a, b): print a, b # normal args
-
-
def f2(a, *b): print a, b # positional varargs
-
-
f3(a, **b): print a, b # keyword varargs
-
-
f4(a, *b, **c): print a, b, c # mixed modes
-
-
f5(a, b=2, c=3): print a, b, c # defaults
-
-
f6(a, b=2, *c): print a, b, c # defaults + positional varargs
-
% python
-
>>> f1(1, 2) # matched by position (order matters)
-
1 2
-
>>> f1(b=2, a=1) # matched by name (order doesn't matter)
-
1 2
-
>>> f2(1, 2, 3) # extra positionals collected in a tuple
-
1 (2, 3)
-
>>> f3(1, x=2, y=3) # extra keywords collected in a dictionary
-
1 {'x': 2, 'y': 3}
-
>>> f4(1, 2, 3, x=2, y=3) # extra of both kinds
-
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.
Okay, that answers my question and thus the thread dies.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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 ...
|
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...
|
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...
|
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??
|
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...
|
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...
|
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...
|
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: --...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |