Function not working when importing the fileHow do I fix PyDev “Undefined variable from import” errors?Unresolved Import Issues with PyDev and EclipseImporting from another project in pydevUnresolved import:“ execute_from_command_line occured when I worked with the pydev in Eclipse”PyDev is trying to import non-test-related files?“import as” leads to unresolved import error, “from .. import” does notsetting up eclipse for pygame correctly: “unresolved import”Eclipse, pydef, python on usb Stick - import doesn't workPyDev and pygame: Undefined Variable From Import ErrorsHow to return variables or even values through classes or even functions with attributes in Python?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

how do we prove that a sum of two periods is still a period?

Does int main() need a declaration on C++?

Can compressed videos be decoded back to their uncompresed original format?

How to show a landlord what we have in savings?

OP Amp not amplifying audio signal

How to prevent "they're falling in love" trope

What Exploit Are These User Agents Trying to Use?

Processor speed limited at 0.4 Ghz

Bullying boss launched a smear campaign and made me unemployable

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

Placement of More Information/Help Icon button for Radio Buttons

Are British MPs missing the point, with these 'Indicative Votes'?

Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)

Avoiding the "not like other girls" trope?

Is there a hemisphere-neutral way of specifying a season?

Should I tell management that I intend to leave due to bad software development practices?

Is this draw by repetition?

How dangerous is XSS

How can I prove that a state of equilibrium is unstable?

In the UK, is it possible to get a referendum by a court decision?

Theorists sure want true answers to this!

Is it possible to map the firing of neurons in the human brain so as to stimulate artificial memories in someone else?

Is it inappropriate for a student to attend their mentor's dissertation defense?



Function not working when importing the file


How do I fix PyDev “Undefined variable from import” errors?Unresolved Import Issues with PyDev and EclipseImporting from another project in pydevUnresolved import:“ execute_from_command_line occured when I worked with the pydev in Eclipse”PyDev is trying to import non-test-related files?“import as” leads to unresolved import error, “from .. import” does notsetting up eclipse for pygame correctly: “unresolved import”Eclipse, pydef, python on usb Stick - import doesn't workPyDev and pygame: Undefined Variable From Import ErrorsHow to return variables or even values through classes or even functions with attributes in Python?













0















So I have multiple files for different functions I am using for a game. The file that I am having problems with is a function I created to add buttons to the game. I import this file into another file needing to use the button function but it crashes the program saying "Button is undefined" when it is clearly defined as a function from the function file. Anyone know how to fix this?



Here is the button code:



def Button(Text, X, Y, Width, Height, Inactive_Colour, Active_Colour, Action = None):
Cursor = pg.mouse.get_pos()

Click = pg.mouse.get_pressed()

if X + Width > Cursor[0] > X and Y + Height > Cursor[1] > Y:
pg.draw.rect(Display, Active_Colour, (X, Y, Width, Height))
if Click[0] == 1 and Action != None:
if Action == "Quit":
pg.quit()
quit()

elif Action == "Controls":
Controls()

elif Action == "Play":
Game_Loop()

elif Action == "Main Menu":
Main_Menu()

elif Action == "Objective":
Objective()

else:
pg.draw.rect(Display, Inactive_Colour, (X, Y, Width, Height))
Text_To_Button(Text, Black, X, Y, Width, Height)


Here is the main menu code that uses the button function from the button file:



import pygame as pg
from Settings import *
from Buttons import *
from Text import *

def Main_Menu():
Main_Menu = True
while Main_Menu:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()

elif event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
Main_Menu = False

elif event.key == pg.K_ESCAPE:
pg.quit()
quit()

Display.fill(White)

Message_To_Screen("RPG", Light_Green, -180, Size = "Large")

# Message_To_Screen("Press Return to play, P to pause, or Escape to quit.", BLACK, 180, Size = "Small")
Button("Play", 25, 500, 150, 50, Dark_Green, Light_Green, Action = "Play")
Button("Controls", 225, 500, 150, 50, Dark_Cyan, Light_Cyan, Action = "Controls")
Button("Objective", 425, 500, 150, 50, Dark_Yellow, Light_Yellow, Action = "Objective")
Button("Quit", 625, 500, 150, 50, Dark_Red, Light_Red, Action = "Quit")

pg.display.update()

Clock.tick(15)

Main_Menu()


The button function works when it is in the same file but doesn't when I import it from a different file as if it doesn't exist when it does.










share|improve this question
























  • How do you import the file which contains Button? If you do import filename then change it to from filename import *

    – Rabbid76
    Mar 8 at 21:30












  • I do from filename import * but it doesn't work the way it's suppose to. I even tried just importing filename but still not identified

    – Adam Sabadash
    Mar 8 at 23:27












  • Please include the import statements in your question test. If you're button class is in button.py, then from button import * will work.

    – Kingsley
    Mar 10 at 21:27











  • It is in py format but importing it will not work

    – Adam Sabadash
    Mar 12 at 14:13











  • Updated the code in the question to include the imports

    – Adam Sabadash
    Mar 12 at 15:21















0















So I have multiple files for different functions I am using for a game. The file that I am having problems with is a function I created to add buttons to the game. I import this file into another file needing to use the button function but it crashes the program saying "Button is undefined" when it is clearly defined as a function from the function file. Anyone know how to fix this?



Here is the button code:



def Button(Text, X, Y, Width, Height, Inactive_Colour, Active_Colour, Action = None):
Cursor = pg.mouse.get_pos()

Click = pg.mouse.get_pressed()

if X + Width > Cursor[0] > X and Y + Height > Cursor[1] > Y:
pg.draw.rect(Display, Active_Colour, (X, Y, Width, Height))
if Click[0] == 1 and Action != None:
if Action == "Quit":
pg.quit()
quit()

elif Action == "Controls":
Controls()

elif Action == "Play":
Game_Loop()

elif Action == "Main Menu":
Main_Menu()

elif Action == "Objective":
Objective()

else:
pg.draw.rect(Display, Inactive_Colour, (X, Y, Width, Height))
Text_To_Button(Text, Black, X, Y, Width, Height)


Here is the main menu code that uses the button function from the button file:



import pygame as pg
from Settings import *
from Buttons import *
from Text import *

def Main_Menu():
Main_Menu = True
while Main_Menu:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()

elif event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
Main_Menu = False

elif event.key == pg.K_ESCAPE:
pg.quit()
quit()

Display.fill(White)

Message_To_Screen("RPG", Light_Green, -180, Size = "Large")

# Message_To_Screen("Press Return to play, P to pause, or Escape to quit.", BLACK, 180, Size = "Small")
Button("Play", 25, 500, 150, 50, Dark_Green, Light_Green, Action = "Play")
Button("Controls", 225, 500, 150, 50, Dark_Cyan, Light_Cyan, Action = "Controls")
Button("Objective", 425, 500, 150, 50, Dark_Yellow, Light_Yellow, Action = "Objective")
Button("Quit", 625, 500, 150, 50, Dark_Red, Light_Red, Action = "Quit")

pg.display.update()

Clock.tick(15)

Main_Menu()


The button function works when it is in the same file but doesn't when I import it from a different file as if it doesn't exist when it does.










share|improve this question
























  • How do you import the file which contains Button? If you do import filename then change it to from filename import *

    – Rabbid76
    Mar 8 at 21:30












  • I do from filename import * but it doesn't work the way it's suppose to. I even tried just importing filename but still not identified

    – Adam Sabadash
    Mar 8 at 23:27












  • Please include the import statements in your question test. If you're button class is in button.py, then from button import * will work.

    – Kingsley
    Mar 10 at 21:27











  • It is in py format but importing it will not work

    – Adam Sabadash
    Mar 12 at 14:13











  • Updated the code in the question to include the imports

    – Adam Sabadash
    Mar 12 at 15:21













0












0








0








So I have multiple files for different functions I am using for a game. The file that I am having problems with is a function I created to add buttons to the game. I import this file into another file needing to use the button function but it crashes the program saying "Button is undefined" when it is clearly defined as a function from the function file. Anyone know how to fix this?



Here is the button code:



def Button(Text, X, Y, Width, Height, Inactive_Colour, Active_Colour, Action = None):
Cursor = pg.mouse.get_pos()

Click = pg.mouse.get_pressed()

if X + Width > Cursor[0] > X and Y + Height > Cursor[1] > Y:
pg.draw.rect(Display, Active_Colour, (X, Y, Width, Height))
if Click[0] == 1 and Action != None:
if Action == "Quit":
pg.quit()
quit()

elif Action == "Controls":
Controls()

elif Action == "Play":
Game_Loop()

elif Action == "Main Menu":
Main_Menu()

elif Action == "Objective":
Objective()

else:
pg.draw.rect(Display, Inactive_Colour, (X, Y, Width, Height))
Text_To_Button(Text, Black, X, Y, Width, Height)


Here is the main menu code that uses the button function from the button file:



import pygame as pg
from Settings import *
from Buttons import *
from Text import *

def Main_Menu():
Main_Menu = True
while Main_Menu:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()

elif event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
Main_Menu = False

elif event.key == pg.K_ESCAPE:
pg.quit()
quit()

Display.fill(White)

Message_To_Screen("RPG", Light_Green, -180, Size = "Large")

# Message_To_Screen("Press Return to play, P to pause, or Escape to quit.", BLACK, 180, Size = "Small")
Button("Play", 25, 500, 150, 50, Dark_Green, Light_Green, Action = "Play")
Button("Controls", 225, 500, 150, 50, Dark_Cyan, Light_Cyan, Action = "Controls")
Button("Objective", 425, 500, 150, 50, Dark_Yellow, Light_Yellow, Action = "Objective")
Button("Quit", 625, 500, 150, 50, Dark_Red, Light_Red, Action = "Quit")

pg.display.update()

Clock.tick(15)

Main_Menu()


The button function works when it is in the same file but doesn't when I import it from a different file as if it doesn't exist when it does.










share|improve this question
















So I have multiple files for different functions I am using for a game. The file that I am having problems with is a function I created to add buttons to the game. I import this file into another file needing to use the button function but it crashes the program saying "Button is undefined" when it is clearly defined as a function from the function file. Anyone know how to fix this?



Here is the button code:



def Button(Text, X, Y, Width, Height, Inactive_Colour, Active_Colour, Action = None):
Cursor = pg.mouse.get_pos()

Click = pg.mouse.get_pressed()

if X + Width > Cursor[0] > X and Y + Height > Cursor[1] > Y:
pg.draw.rect(Display, Active_Colour, (X, Y, Width, Height))
if Click[0] == 1 and Action != None:
if Action == "Quit":
pg.quit()
quit()

elif Action == "Controls":
Controls()

elif Action == "Play":
Game_Loop()

elif Action == "Main Menu":
Main_Menu()

elif Action == "Objective":
Objective()

else:
pg.draw.rect(Display, Inactive_Colour, (X, Y, Width, Height))
Text_To_Button(Text, Black, X, Y, Width, Height)


Here is the main menu code that uses the button function from the button file:



import pygame as pg
from Settings import *
from Buttons import *
from Text import *

def Main_Menu():
Main_Menu = True
while Main_Menu:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()

elif event.type == pg.KEYDOWN:
if event.key == pg.K_RETURN:
Main_Menu = False

elif event.key == pg.K_ESCAPE:
pg.quit()
quit()

Display.fill(White)

Message_To_Screen("RPG", Light_Green, -180, Size = "Large")

# Message_To_Screen("Press Return to play, P to pause, or Escape to quit.", BLACK, 180, Size = "Small")
Button("Play", 25, 500, 150, 50, Dark_Green, Light_Green, Action = "Play")
Button("Controls", 225, 500, 150, 50, Dark_Cyan, Light_Cyan, Action = "Controls")
Button("Objective", 425, 500, 150, 50, Dark_Yellow, Light_Yellow, Action = "Objective")
Button("Quit", 625, 500, 150, 50, Dark_Red, Light_Red, Action = "Quit")

pg.display.update()

Clock.tick(15)

Main_Menu()


The button function works when it is in the same file but doesn't when I import it from a different file as if it doesn't exist when it does.







pygame pydev






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 12 at 14:30







Adam Sabadash

















asked Mar 8 at 21:00









Adam SabadashAdam Sabadash

84




84












  • How do you import the file which contains Button? If you do import filename then change it to from filename import *

    – Rabbid76
    Mar 8 at 21:30












  • I do from filename import * but it doesn't work the way it's suppose to. I even tried just importing filename but still not identified

    – Adam Sabadash
    Mar 8 at 23:27












  • Please include the import statements in your question test. If you're button class is in button.py, then from button import * will work.

    – Kingsley
    Mar 10 at 21:27











  • It is in py format but importing it will not work

    – Adam Sabadash
    Mar 12 at 14:13











  • Updated the code in the question to include the imports

    – Adam Sabadash
    Mar 12 at 15:21

















  • How do you import the file which contains Button? If you do import filename then change it to from filename import *

    – Rabbid76
    Mar 8 at 21:30












  • I do from filename import * but it doesn't work the way it's suppose to. I even tried just importing filename but still not identified

    – Adam Sabadash
    Mar 8 at 23:27












  • Please include the import statements in your question test. If you're button class is in button.py, then from button import * will work.

    – Kingsley
    Mar 10 at 21:27











  • It is in py format but importing it will not work

    – Adam Sabadash
    Mar 12 at 14:13











  • Updated the code in the question to include the imports

    – Adam Sabadash
    Mar 12 at 15:21
















How do you import the file which contains Button? If you do import filename then change it to from filename import *

– Rabbid76
Mar 8 at 21:30






How do you import the file which contains Button? If you do import filename then change it to from filename import *

– Rabbid76
Mar 8 at 21:30














I do from filename import * but it doesn't work the way it's suppose to. I even tried just importing filename but still not identified

– Adam Sabadash
Mar 8 at 23:27






I do from filename import * but it doesn't work the way it's suppose to. I even tried just importing filename but still not identified

– Adam Sabadash
Mar 8 at 23:27














Please include the import statements in your question test. If you're button class is in button.py, then from button import * will work.

– Kingsley
Mar 10 at 21:27





Please include the import statements in your question test. If you're button class is in button.py, then from button import * will work.

– Kingsley
Mar 10 at 21:27













It is in py format but importing it will not work

– Adam Sabadash
Mar 12 at 14:13





It is in py format but importing it will not work

– Adam Sabadash
Mar 12 at 14:13













Updated the code in the question to include the imports

– Adam Sabadash
Mar 12 at 15:21





Updated the code in the question to include the imports

– Adam Sabadash
Mar 12 at 15:21












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070937%2ffunction-not-working-when-importing-the-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070937%2ffunction-not-working-when-importing-the-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived