1

i am trying to open settings.py in main.py by importing it like a library, but whenever i run main.py, the setting page opens immediately and im not sure why

whats is meant to happen is when you press the Account button you are supposed to run the settings page and get that the to load however that doesnt happen, instead whenever you run main.py settings.py instantly gets run without the main menu loading. This worked fine with the button but it doesnt work with the settings page i have tried to use exec and os to open it instead but that doesnt work.

the myLibrary is just the button defintion i used to create the account button

main.py

import pygame, sys, os
import myLibrary as lib
import settings

def runSettings():
    print(settings.mainloop())
    return

script_dir = sys.path[0]

bg_path = os.path.join(script_dir, '../info/images/backgrounds/MainMenu background.png')
bg = pygame.image.load(bg_path)

(width, height) = (1300, 790)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Main Menu')

Account_img = os.path.join(script_dir, '../info/images/MainMenu buttons/account.png')
Account_btn = lib.Button(Account_img, (180,340), runSettings)

while True:

    screen.blit(bg, (0, 0))
    screen.blit(Account_btn.image, Account_btn.rect)

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            Account_btn.on_click(event)

    pygame.display.update()

settings.py

import pygame, sys, os
import myLibrary as lib

script_dir = sys.path[0]

bg_path = os.path.join(script_dir, '../info/images/backgrounds/Settings background.png')
bg = pygame.image.load(bg_path)

(width, height) = (1100, 700)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Settings')


#titles

Username_img = pygame.image.load(os.path.join(script_dir, '../info/images/Settings buttons/username.png'))

def mainloop():

    while True:

        screen.blit(bg, (0,0))
        screen.blit(Username_img, (30,20))

        for event in pygame.event.get():
            pass

        pygame.display.update()
                
mainloop()

button definition, myLibrary as lib

import pygame


#button class will take the image, the position and callback
#function to create and place buttons
class Button:
    def __init__(self, image, position, callback):
        self.image = pygame.image.load(image)
        self.image.convert()
        self.rect = self.image.get_rect(topleft=position)
        self.callback = callback
 
    def on_click(self, event):
        if event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.callback()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • This is a typical run from main/run from import issue. Your codes misses a ```if __name__ == '__main__'``` block to prevent code to run at import time. See https://stackoverflow.com/questions/419163/what-does-if-name-main-do/419185#419185 – Stefan Dec 07 '21 at 11:52
  • thanks! this is exactly what im looking for, it has worked after putting in the if statements in both scripts – twisting234 Dec 07 '21 at 14:28

0 Answers0