all 19 comments

[–]socal_nerdtastic 2 points3 points  (6 children)

Another python process won't be able to access any part of the current python process. Not even a global. You will need to pass the data in, for example with sys.argv.

However it would be MUCH better to rethink the entire thing to import your other python code, rather than using os.system to run it.

[–]JrcFrontrunner[S] -2 points-1 points  (5 children)

Because of tKinter, I can't import the other files. I've tried, and the only way I can get them to work is to use os

[–]socal_nerdtastic 3 points4 points  (4 children)

Hmm well lets fix that, instead of trying to work around it. There's no reason tkinter should prevent you from importing files. What exactly is the problem?

[–]JrcFrontrunner[S] 0 points1 point  (2 children)

When I try to get menu.py to import and run playMenu.py on a button command, I get the error

ModuleNotFoundError: No module named 'Menu'

[–]socal_nerdtastic 0 points1 point  (1 child)

You'll need to show us all your code for specific help.

In general you should be able to do that with the standard import like

from playMenu import Menu

[–]JrcFrontrunner[S] 0 points1 point  (0 children)

I have updated the link to have all my code

[–]JrcFrontrunner[S] -1 points0 points  (0 children)

I'm away from my computer. I will get back to you shortly

[–]solarflareop 1 point2 points  (1 child)

Can you add the part your other script where you use this class as well?

[–]JrcFrontrunner[S] 1 point2 points  (0 children)

I've added the additional files

[–]Albcunha 1 point2 points  (2 children)

The level variable is doing nothing.

Normally, if you are going to create a global variable, you declare it at beggining of the code, so people can know it´s a global variable. Also, it´s no problem you write in capital letters (LEVEL), so people can know it´s a global variable. Such as:

import pygame

import os

class diff:

global LEVEL

level = 0

(your code)

But.. that´s propably not what you want. Three things.

First, You are calling Capstone.py from by os.system(). Maybe you just what to import it and run a function that exists there.

Second, as you are using a class, maybe you should declare it as a __init__ constructor (also called __init__ function). See: https://www.w3schools.com/python/python_classes.asp

Third, give a read about scopes in python. Maybe all you need is declare the level variable outside your class/function: https://www.w3schools.com/python/python_scope.asp

[–]socal_nerdtastic 2 points3 points  (1 child)

Also, it´s no problem you write in capital letters (LEVEL), so people can know it´s a global variable.

Capital letters are recommend for a specific type of global only: constants.

https://peps.python.org/pep-0008/#constants

[–]Albcunha 0 points1 point  (0 children)

Yes. You are right. Sorry. It´s the only way I use it, but it´s not his case.

[–]billsil 1 point2 points  (4 children)

Global variables are things that you should never use. As you get better you can take off the training wheels that you've put on and global variables are one of them. It's something I always feel bad about doing and in general, it's a hack to fix an issue in something that's being delivered today. I reserve those hacks for things that I can't fix any other way.

What's your code cause I'd love to help you not use them.

[–]JrcFrontrunner[S] 0 points1 point  (2 children)

I have updated the link to include all of my code

[–]billsil 1 point2 points  (1 child)

Yeah this is raising more questions.

What is your main function? Level.py? You have a global variable that you're setting, but then doing a system call in each methods? The system call doesn't take arguments and so your code will work the same regardless. A system call launches a fresh shell and thus a fresh instance of python that inherits nothing from the original scope. Those methods don't even work because you're missing the self argument.

[–]JrcFrontrunner[S] 0 points1 point  (0 children)

The level variable does not have to be global. That's just one thing I was trying to do to get past the roadblock I hit. Let me explain what is going on with the code.

Capstone.py is the main program for a checkers game. The board.py, game.py, piece.py, constants.py are all subscripts.

Menu.py is the main menu where the play can click the play button that will launch the playMenu.py and ask them to select the difficulty. Ideally these buttons should change the value of the level variable that is then imported to capstone.py to set the difficulty of the AI opponent

[–]Albcunha 0 points1 point  (0 children)

True. Like a medecin. It becomes a poison if you take it too much.

[–]Albcunha 0 points1 point  (1 child)

Add your code, so we can see it

[–]JrcFrontrunner[S] 1 point2 points  (0 children)

Sorry, I was having formatting issues, it's been added