473,785 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

importing class

Hi all,

I'm trying to import a class from a module. The class looks like this:
class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.button = Button(frame, text=text_1, command= self.comm_1)
self.button.pac k(side=LEFT)

self.hi_there = Button(frame, text=text_2, command=self.co mm_2)
self.hi_there.p ack(side=LEFT)

def comm_1(self):
command1()
root.quit()

def comm_2(self):
command2()
root.quit()

It's supposed to just make a Tkinter window with two choices. The
problem is that when I import it from a module, I get the following
error:

NameError: global name 'Frame' is not defined

But when I copy and paste it into the file, it works. Can anyone tell
me what's wrong?

Greg

Oct 27 '06 #1
6 2572
In <11************ *********@b28g2 000cwb.googlegr oups.com>, gmarkowsky
wrote:
Hi all,

I'm trying to import a class from a module. The class looks like this:
class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.button = Button(frame, text=text_1, command= self.comm_1)
self.button.pac k(side=LEFT)

self.hi_there = Button(frame, text=text_2, command=self.co mm_2)
self.hi_there.p ack(side=LEFT)

def comm_1(self):
command1()
root.quit()

def comm_2(self):
command2()
root.quit()

It's supposed to just make a Tkinter window with two choices. The
problem is that when I import it from a module, I get the following
error:

NameError: global name 'Frame' is not defined

But when I copy and paste it into the file, it works. Can anyone tell
me what's wrong?
Yes, the global name `Frame` is not defined. `Frame` is a name in the
`Tkinter` module and you have to import it to reference it. Add the
following import statement to your file:

from Tkinter import Frame, Button

You use `Button` too and this also lives in the `Tkinter` module.

Ciao,
Marc 'BlackJack' Rintsch
Oct 27 '06 #2
Yep, that fixed it. Many thanks.

Greg

Dennis Lee Bieber wrote:
On 27 Oct 2006 09:22:00 -0700, gm********@gmai l.com declaimed the
following in comp.lang.pytho n:
It's supposed to just make a Tkinter window with two choices. The
problem is that when I import it from a module, I get the following
error:

NameError: global name 'Frame' is not defined

But when I copy and paste it into the file, it works. Can anyone tell
me what's wrong?
Probably the simple fact that your "file" likely has all the imports
for Tkinter defined. The module that you are importing needs to have
those imports inside it -- imported modules do not have visibility of
names defined in the importING file.
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netc om.com wu******@bestia ria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestia ria.com)
HTTP://www.bestiaria.com/
Oct 29 '06 #3
Thanks, I got that part. The problem I'm still having is that it's not
seeing things like text_1, which are defined in the program. How can I
make it see that?

Another question I should ask is whether I should even bother doing
this. That is, it seems that the elegant and approved way of doing this
kind of thing may be to put a class in a module and then just use the
module over and over again in programs. I'm making a few GUIs which
present two options and ask the user to chose one, so I thought I could
just do it this way. Of course I could very easily just copy and paste
the class into each file, but that seems silly. I haven't had any
trouble using modules for functions, but for classes it is not working
right so far, and I'm having trouble finding examples to follow.

Greg

Marc 'BlackJack' Rintsch wrote:
In <11************ *********@b28g2 000cwb.googlegr oups.com>, gmarkowsky
wrote:
Hi all,

I'm trying to import a class from a module. The class looks like this:
class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.button = Button(frame, text=text_1, command= self.comm_1)
self.button.pac k(side=LEFT)

self.hi_there = Button(frame, text=text_2, command=self.co mm_2)
self.hi_there.p ack(side=LEFT)

def comm_1(self):
command1()
root.quit()

def comm_2(self):
command2()
root.quit()

It's supposed to just make a Tkinter window with two choices. The
problem is that when I import it from a module, I get the following
error:

NameError: global name 'Frame' is not defined

But when I copy and paste it into the file, it works. Can anyone tell
me what's wrong?

Yes, the global name `Frame` is not defined. `Frame` is a name in the
`Tkinter` module and you have to import it to reference it. Add the
following import statement to your file:

from Tkinter import Frame, Button

You use `Button` too and this also lives in the `Tkinter` module.

Ciao,
Marc 'BlackJack' Rintsch
Oct 29 '06 #4
gm********@gmai l.com wrote:
Thanks, I got that part. The problem I'm still having is that it's not
seeing things like text_1, which are defined in the program. How can I
make it see that?
Your module is intended to work with many different main programs, so it
shouldn't make any assumptions about the names that the main program
uses for things. That would be rather bad programming style ("rigind
coupling" is something to be avoided where possible). I wouldn't call
that class App just because it's misleading: maybe you could change the
name to YesNo, or Choice, or something more indicative of its function?

You could pass text_1 and text_2 as arguments to the class's __init__
method - that way you could just use them directly.
Another question I should ask is whether I should even bother doing
this. That is, it seems that the elegant and approved way of doing this
kind of thing may be to put a class in a module and then just use the
module over and over again in programs. I'm making a few GUIs which
present two options and ask the user to chose one, so I thought I could
just do it this way. Of course I could very easily just copy and paste
the class into each file, but that seems silly. I haven't had any
trouble using modules for functions, but for classes it is not working
right so far, and I'm having trouble finding examples to follow.
Seems like parameterizatio n is the thing you are missing. Change the
__init__ method declaration to

def __init__(self, master, text_1="OK", text_2="Cancel" ):
...

leaving the rest of the code the same. (Though I note your module also
fails to define a "command1" and "command2" function, this may just be
because you are only quoting partial code).

Then in your main program create the object with

myDialog = YesNo(master, "Yes", "No")

Looks like you are new to Python - perseverre and you will pick it up
quite quickly!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 30 '06 #5
Thanks for your help. Actually my idea was that command1 and command2
would be defined within the program, not the module, as I would have
different choices in different programs. Should I pass them in as a
parameter too?

Greg

Steve Holden wrote:
gm********@gmai l.com wrote:
Thanks, I got that part. The problem I'm still having is that it's not
seeing things like text_1, which are defined in the program. How can I
make it see that?
Your module is intended to work with many different main programs, so it
shouldn't make any assumptions about the names that the main program
uses for things. That would be rather bad programming style ("rigind
coupling" is something to be avoided where possible). I wouldn't call
that class App just because it's misleading: maybe you could change the
name to YesNo, or Choice, or something more indicative of its function?

You could pass text_1 and text_2 as arguments to the class's __init__
method - that way you could just use them directly.
Another question I should ask is whether I should even bother doing
this. That is, it seems that the elegant and approved way of doing this
kind of thing may be to put a class in a module and then just use the
module over and over again in programs. I'm making a few GUIs which
present two options and ask the user to chose one, so I thought I could
just do it this way. Of course I could very easily just copy and paste
the class into each file, but that seems silly. I haven't had any
trouble using modules for functions, but for classes it is not working
right so far, and I'm having trouble finding examples to follow.
Seems like parameterizatio n is the thing you are missing. Change the
__init__ method declaration to

def __init__(self, master, text_1="OK", text_2="Cancel" ):
...

leaving the rest of the code the same. (Though I note your module also
fails to define a "command1" and "command2" function, this may just be
because you are only quoting partial code).

Then in your main program create the object with

myDialog = YesNo(master, "Yes", "No")

Looks like you are new to Python - perseverre and you will pick it up
quite quickly!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
Oct 30 '06 #6
gm********@gmai l.com wrote:
Thanks for your help. Actually my idea was that command1 and command2
would be defined within the program, not the module, as I would have
different choices in different programs. Should I pass them in as a
parameter too?
It would seem to be the only way to tell your object which callbacks to
call!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1317
by: Thomas Aanensen | last post by:
Hi all. I'm sending a class over a socket connection, and I want to use this class at the new location. The problem is that I get a "module blah does not exist" error. Is it possible for me to use this class without importing the module? How? Any other suggestions?
1
1391
by: Gabriele *darkbard* Farina | last post by:
Hi I've my application filesystem structured like this: app/ -main.py -pharaon/ --__init__.py --engine/ ---__init__.py
12
2406
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In some cases there is a further complication: module importing through an indirect mechanism, like: exec "from " + xxx + " import *". A part the fact that I have not understood the "real" difference between import and from ... import (or also from......
1
2115
by: em | last post by:
Hi all, I'm getting some problems importing a DLL that I made in C# within VB6.0. The C# is quite easy, just for trying: namespace TestDll { public class Class1
0
957
by: Ivan Lam | last post by:
Hi All, Thanks for reading my question. I am a newbie of VC .Net, and I am facing a problem about importing DLL. I have searched from the web and I download a simple sample. And the code is as follow:
29
4229
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Caching;
2
2513
by: dawg1998 | last post by:
I am setting up a new web application with ASP.NET 2.0 and I was wanting to utilize the 2.0 Membership and Profiles system. However, I am having a lot of trouble importing a table of user info into the aspnet_Users table due to the unique identifiers and large number of dependencies this new system has. Is there any way to easily import exisiting user data into the 2.0 Membership system without having to do data entry for 200+ users? ...
7
1965
by: hg | last post by:
Hi, I have the following problem. I find in a directory hierarchy some files following a certain sets of rules: ..../.../../plugin/name1/name1.py ..... ..../.../../plugin/namen/namen.py
2
1365
by: HMS Surprise | last post by:
Greetings, First I will admit I am new to Python but have experience with C++ and some Tcl/Tk. I am starting to use a tool called MaxQ that uses jython. I have been studying Rossum's tutorial but still am unclear on importing, the use of self, and calling functions with their own name in quotes. In the code below the lines between #Start and #End are generated by browsing web pages. In a large test this section can become quite large....
3
1438
by: stmfc | last post by:
how can a multithreaded code can be compiled succesfully without importing Thread class? the example below is taken from marcus green mock exam 1: public class Rpcraven{ public static void main(String argv){ Pmcraven pm1 = new Pmcraven("One"); pm1.run(); Pmcraven pm2 = new Pmcraven("Two"); pm2.run();
0
9645
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10095
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6741
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.