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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page